Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fail tests when multiple done() calls are made #10624

Merged
merged 22 commits into from Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 26 additions & 9 deletions e2e/__tests__/__snapshots__/callDoneTwice.test.ts.snap
Expand Up @@ -2,18 +2,35 @@

exports[`\`done()\` should not be called more than once 1`] = `
FAIL __tests__/index.test.js
✕ \`done()\` should not be called more than once
\`done()\` called more than once
✕ should fail
✕ should fail inside a promise

● \`done()\` should not be called more than once
● \`done()\` called more than once › should fail

Expected done to be called once, but it was called multiple times.

7 | test('\`done()\` should not be called more than once', done => {
8 | done();
> 9 | done();
| ^
10 | });
11 |
8 | it('should fail', done => {
9 | done();
> 10 | done();
| ^
11 | });
12 |
13 | it('should fail inside a promise', done => {

at Object.done (__tests__/index.test.js:9:3)
at Object.done (__tests__/index.test.js:10:5)

● \`done()\` called more than once › should fail inside a promise

Expected done to be called once, but it was called multiple times.

15 | .then(() => {
16 | done();
> 17 | done();
| ^
18 | })
19 | .catch(err => err);
20 | });

at done (__tests__/index.test.js:17:9)
`;
5 changes: 5 additions & 0 deletions packages/jest-circus/src/eventHandler.ts
Expand Up @@ -31,6 +31,7 @@ const eventHandler: Circus.EventHandler = (
break;
}
case 'hook_start': {
event.hook.seenDone = false;
break;
}
case 'start_describe_definition': {
Expand Down Expand Up @@ -195,6 +196,10 @@ const eventHandler: Circus.EventHandler = (
event.test.invocations += 1;
break;
}
case 'test_fn_start': {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
event.test.seenDone = false;
break;
}
case 'test_fn_failure': {
const {
error,
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-circus/src/run.ts
Expand Up @@ -149,7 +149,6 @@ const _callCircusHook = async ({
testContext?: Circus.TestContext;
}): Promise<void> => {
await dispatch({hook, name: 'hook_start'});
hook.seenDone = false;
const timeout = hook.timeout || getState().testTimeout;

try {
Expand All @@ -168,7 +167,6 @@ const _callCircusTest = async (
testContext: Circus.TestContext,
): Promise<void> => {
await dispatch({name: 'test_fn_start', test});
test.seenDone = false;
const timeout = test.timeout || getState().testTimeout;
invariant(test.fn, `Tests with no 'fn' should have 'mode' set to 'skipped'`);

Expand Down
14 changes: 8 additions & 6 deletions packages/jest-circus/src/utils.ts
Expand Up @@ -192,20 +192,22 @@ export const callAsyncCircusFn = (
let returnedValue: unknown = undefined;

const done = (reason?: Error | string): void => {
// We need to keep a stack here before the promise tick
const errorAtDone = new ErrorWithStack(undefined, done);

if (!completed && testOrHook.seenDone) {
let message =
errorAtDone.message =
'Expected done to be called once, but it was called multiple times.';

if (reason) {
message += ' Reason: ' + prettyFormat(reason, {maxDepth: 3});
errorAtDone.message +=
' Reason: ' + prettyFormat(reason, {maxDepth: 3});
}

throw new ErrorWithStack(message, done);
reject(errorAtDone);
throw errorAtDone;
} else {
testOrHook.seenDone = true;
}
// We need to keep a stack here before the promise tick
const errorAtDone = new ErrorWithStack(undefined, done);

// Use `Promise.resolve` to allow the event loop to go a single tick in case `done` is called synchronously
Promise.resolve().then(() => {
Expand Down