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: explicitly mention that test is failing because done() is not being called #13847

Merged
merged 9 commits into from
Feb 2, 2023
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[expect, @jest/expect]` Provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions ([#13848](https://github.com/facebook/jest/pull/13848))
- `[jest-circus]` Added explicit mention of test failing because `done()` is not being called in error message ([#13847](https://github.com/facebook/jest/pull/13847))

### Chore & Maintenance

Expand Down
8 changes: 8 additions & 0 deletions e2e/__tests__/__snapshots__/timeouts.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites."
`;

exports[`exceeds the timeout specifying that \`done\` has not been called 1`] = `
"Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites."
`;
21 changes: 21 additions & 0 deletions e2e/__tests__/timeouts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,24 @@ test('does not exceed the command line testTimeout', () => {
expect(summary).toMatchSnapshot();
expect(exitCode).toBe(0);
});

test('exceeds the timeout specifying that `done` has not been called', () => {
writeFiles(DIR, {
'__tests__/a-banana.js': `
jest.setTimeout(20);

test('banana', (done) => {
expect(1 + 1).toBe(2);
});
`,
'package.json': '{}',
});

const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']);
const {rest, summary} = extractSummary(stderr);
expect(rest).toMatch(
/(jest\.setTimeout|Exceeded timeout\.while waiting for `done()` to be called)/,
);
expect(summary).toMatchSnapshot();
expect(exitCode).toBe(1);
});
13 changes: 10 additions & 3 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,15 @@ export const describeBlockHasTests = (
child => child.type === 'test' || describeBlockHasTests(child),
);

const _makeTimeoutMessage = (timeout: number, isHook: boolean) =>
const _makeTimeoutMessage = (
timeout: number,
isHook: boolean,
takesDoneCallback: boolean,
) =>
`Exceeded timeout of ${formatTime(timeout)} for a ${
isHook ? 'hook' : 'test'
}${
takesDoneCallback && ' while waiting for `done()` to be called'
}.\nUse jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test.`;

// Global values can be overwritten by mocks or tests. We'll capture
Expand All @@ -193,16 +199,17 @@ export const callAsyncCircusFn = (
let completed = false;

const {fn, asyncError} = testOrHook;
const doneCallback = takesDoneCallback(fn);

return new Promise<void>((resolve, reject) => {
timeoutID = setTimeout(
() => reject(_makeTimeoutMessage(timeout, isHook)),
() => reject(_makeTimeoutMessage(timeout, isHook, doneCallback)),
timeout,
);

// If this fn accepts `done` callback we return a promise that fulfills as
// soon as `done` called.
if (takesDoneCallback(fn)) {
if (doneCallback) {
let returnedValue: unknown = undefined;

const done = (reason?: Error | string): void => {
Expand Down