diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ac7e81d9a9c..f4f5bbe26c2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - `[jest-circus]` Send test case results for `todo` tests ([#13915](https://github.com/facebook/jest/pull/13915)) - `[jest-circus]` Update message printed on test timeout ([#13830](https://github.com/facebook/jest/pull/13830)) +- `[jest-circus]` Avoid creating the word "testfalse" when `takesDoneCallback` is `false` in the message printed on test timeout AND updated timeouts test ([#13954](https://github.com/facebook/jest/pull/13954)) - `[@jest/test-result]` Allow `TestResultsProcessor` type to return a Promise ([#13950](https://github.com/facebook/jest/pull/13950)) ### Chore & Maintenance diff --git a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap index 70a79392c7cc..b68b979b731b 100644 --- a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap +++ b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap @@ -26,6 +26,19 @@ Time: <> Ran all test suites." `; +exports[`does not exceed the timeout parameter 1`] = ` +"PASS __tests__/a-banana.js + ✓ banana" +`; + +exports[`does not exceed the timeout parameter 2`] = ` +"Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites." +`; + exports[`exceeds the command line testTimeout 1`] = ` "Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total @@ -42,6 +55,14 @@ Time: <> Ran all test suites." `; +exports[`exceeds the timeout parameter 1`] = ` +"Test Suites: 1 failed, 1 total +Tests: 1 failed, 1 total +Snapshots: 0 total +Time: <> +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 diff --git a/e2e/__tests__/timeouts.test.ts b/e2e/__tests__/timeouts.test.ts index 8a462042ed1f..d369665f5f5b 100644 --- a/e2e/__tests__/timeouts.test.ts +++ b/e2e/__tests__/timeouts.test.ts @@ -30,9 +30,13 @@ test('exceeds the timeout', () => { const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); const {rest, summary} = extractSummary(stderr); - expect(rest).toMatch( - /(jest\.setTimeout|jasmine\.DEFAULT_TIMEOUT_INTERVAL|Exceeded timeout)/, - ); + const regexToMatch = + process.env.JEST_JASMINE === '1' + ? /(Async callback was not invoked within the 20 ms timeout specified by jest\.setTimeout\.)/ + : /(Exceeded timeout of 20 ms for a test\.)/; + + expect(rest).toMatch(/(jest\.setTimeout\(20\))/); + expect(rest).toMatch(regexToMatch); expect(summary).toMatchSnapshot(); expect(exitCode).toBe(1); }); @@ -77,9 +81,11 @@ test('exceeds the command line testTimeout', () => { '--testTimeout=200', ]); const {rest, summary} = extractSummary(stderr); - expect(rest).toMatch( - /(jest\.setTimeout|jasmine\.DEFAULT_TIMEOUT_INTERVAL|Exceeded timeout)/, - ); + const regexToMatch = + process.env.JEST_JASMINE === '1' + ? /(Async callback was not invoked within the 200 ms timeout specified by jest\.setTimeout\.)/ + : /(Exceeded timeout of 200 ms for a test\.)/; + expect(rest).toMatch(regexToMatch); expect(summary).toMatchSnapshot(); expect(exitCode).toBe(1); }); @@ -108,6 +114,50 @@ test('does not exceed the command line testTimeout', () => { expect(exitCode).toBe(0); }); +test('exceeds the timeout parameter', () => { + writeFiles(DIR, { + '__tests__/a-banana.js': ` + + test('banana', () => { + return new Promise(resolve => { + setTimeout(resolve, 1000); + }); + }, 200); + `, + 'package.json': '{}', + }); + + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); + const {rest, summary} = extractSummary(stderr); + const regexToMatch = + process.env.JEST_JASMINE === '1' + ? /(Async callback was not invoked within the 200 ms timeout specified by jest\.setTimeout\.)/ + : /(Exceeded timeout of 200 ms for a test\.)/; + expect(rest).toMatch(regexToMatch); + expect(summary).toMatchSnapshot(); + expect(exitCode).toBe(1); +}); + +test('does not exceed the timeout parameter', () => { + writeFiles(DIR, { + '__tests__/a-banana.js': ` + + test('banana', () => { + return new Promise(resolve => { + setTimeout(resolve, 200); + }); + }, 1000); + `, + 'package.json': '{}', + }); + + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); + const {rest, summary} = extractSummary(stderr); + expect(rest).toMatchSnapshot(); + expect(summary).toMatchSnapshot(); + expect(exitCode).toBe(0); +}); + test('exceeds the timeout specifying that `done` has not been called', () => { writeFiles(DIR, { '__tests__/a-banana.js': ` @@ -122,9 +172,12 @@ test('exceeds the timeout specifying that `done` has not been called', () => { 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)/, - ); + const regexToMatch = + process.env.JEST_JASMINE === '1' + ? /(Async callback was not invoked within the 20 ms timeout specified by jest\.setTimeout\.)/ + : /(Exceeded timeout of 20 ms for a test while waiting for `done\(\)` to be called\.)/; + expect(rest).toMatch(/(jest\.setTimeout\(20\))/); + expect(rest).toMatch(regexToMatch); expect(summary).toMatchSnapshot(); expect(exitCode).toBe(1); }); diff --git a/packages/jest-circus/src/utils.ts b/packages/jest-circus/src/utils.ts index f59fd2886b80..13cf3b321e05 100644 --- a/packages/jest-circus/src/utils.ts +++ b/packages/jest-circus/src/utils.ts @@ -179,7 +179,7 @@ const _makeTimeoutMessage = ( `Exceeded timeout of ${formatTime(timeout)} for a ${ isHook ? 'hook' : 'test' }${ - takesDoneCallback && ' while waiting for `done()` to be called' + takesDoneCallback ? ' while waiting for `done()` to be called' : '' }.\nAdd a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout.`; // Global values can be overwritten by mocks or tests. We'll capture