From cd6eb3e848839ae8e98adae133c26919bbb1396f Mon Sep 17 00:00:00 2001 From: "depfu[bot]" <23717796+depfu[bot]@users.noreply.github.com> Date: Tue, 26 Apr 2022 19:43:40 +0000 Subject: [PATCH 1/3] Update jest to version 27.4.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7a3606103..9dec855d7 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "istanbul": "^0.4.5", "jasmine": "^4.1.0", "jasmine-core": "^4.0.0", - "jest": "^26.6.3", + "jest": "^27.4.5", "karma": "^6.3.16", "karma-browserstack-launcher": "1.6.0", "karma-chrome-launcher": "3.1.0", From 97f21d4177fb33f07b6066a63ab258ed756df442 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Tue, 26 Apr 2022 22:44:33 +0200 Subject: [PATCH 2/3] Get most of the external tests passing with the non-jasmine based Jest 27 --- lib/notifyPendingPromise.js | 14 +++++++++++--- test/external.spec.js | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/notifyPendingPromise.js b/lib/notifyPendingPromise.js index 10f7ae25b..fbeeec6d5 100644 --- a/lib/notifyPendingPromise.js +++ b/lib/notifyPendingPromise.js @@ -1,4 +1,4 @@ -/* global jasmine */ +/* global jasmine, expect */ let pendingPromisesForTheCurrentTest = []; let afterEachRegistered = false; @@ -42,16 +42,24 @@ function registerAfterEachHook() { // mocha testPassed = this.currentTest.state === 'passed'; displayName = this.currentTest.title; - } else if (typeof currentSpec === 'object') { + } else if (currentSpec && typeof currentSpec === 'object') { testPassed = currentSpec.failedExpectations.length === 0; displayName = currentSpec.fullName; + } else if ( + typeof expect === 'function' && + typeof expect.getState === 'function' + ) { + // Jest's global expect + // https://stackoverflow.com/questions/52788380/get-the-current-test-spec-name-in-jest + testPassed = undefined; // Jest 27+ doesn't expose whether the test passed or failed + displayName = expect.getState().currentTestName; } error = new Error( `${displayName}: You have created a promise that was not returned from the it block` ); } pendingPromisesForTheCurrentTest = []; - if (error && testPassed) { + if (error && testPassed !== false) { throw error; } }); diff --git a/test/external.spec.js b/test/external.spec.js index 536a3e08c..b6b471d21 100644 --- a/test/external.spec.js +++ b/test/external.spec.js @@ -312,7 +312,8 @@ if (typeof process === 'object') { }); }); - it('should not report that a promise was created if the test already failed synchronously', () => { + // We cannot detect this in Jest 27 + it.skip('should not report that a promise was created if the test already failed synchronously', () => { return expect( 'forgotToReturnPendingPromiseFromFailingItBlock', 'executed through jest' From 0204edefca6cffeef5e39d36309ec4ff4be66e06 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 27 Apr 2022 09:08:16 +0200 Subject: [PATCH 3/3] Add workaround to get the test suite passing again --- lib/notifyPendingPromise.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/notifyPendingPromise.js b/lib/notifyPendingPromise.js index fbeeec6d5..6c6e6ac5e 100644 --- a/lib/notifyPendingPromise.js +++ b/lib/notifyPendingPromise.js @@ -29,6 +29,15 @@ function registerAfterEachHook() { if (typeof afterEach === 'function' && !afterEachRegistered) { afterEachRegistered = true; try { + if ( + typeof expect === 'function' && + typeof expect.getState === 'function' && + typeof expect.getState().currentTestName !== 'string' + ) { + // Prevent triggering this error in jest 27+ when a test happens to be running and we didn't get to register the afterEach early: + // Hooks cannot be defined inside tests. Hook of type "afterEach" is nested within "should correctly fetch keys in the absence of symbol support". + return; + } afterEach(function () { let error; let testPassed = true; @@ -69,7 +78,7 @@ function registerAfterEachHook() { } } -// When running in jasmine/node.js, afterEach is available immediately, +// When running in jasmine/node.js or jest, afterEach is available immediately, // but doesn't work within the it block. Register the hook immediately: registerAfterEachHook();