Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/notifyPendingPromise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global jasmine */
/* global jasmine, expect */
let pendingPromisesForTheCurrentTest = [];
let afterEachRegistered = false;

Expand Down Expand Up @@ -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;
Expand All @@ -42,16 +51,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;
}
});
Expand All @@ -61,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();

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion test/external.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down