From 650c278b14d823a5221443492dc4937eccd58dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ro=CC=81bert=20Kiss?= Date: Sun, 12 May 2019 15:21:07 +0200 Subject: [PATCH 1/8] feat: set global timeout in command line argument --- CHANGELOG.md | 1 + TestUtils.ts | 1 + docs/CLI.md | 4 ++ .../__snapshots__/timeouts.test.ts.snap | 34 +++++++++ e2e/__tests__/timeouts.test.ts | 70 +++++++++++++++++++ packages/jest-cli/src/cli/args.ts | 6 ++ packages/jest-config/src/ValidConfig.ts | 1 + .../src/__tests__/normalize.test.js | 16 +++++ packages/jest-config/src/index.ts | 1 + packages/jest-config/src/normalize.ts | 6 ++ .../log_debug_messages.test.ts.snap | 1 + packages/jest-jasmine2/src/index.ts | 1 + .../jest-jasmine2/src/jasmine/jasmineLight.ts | 2 +- packages/jest-types/src/Config.ts | 3 + 14 files changed, 146 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13bd95c916d4..f65d3dd02a69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[expect]` Highlight substring differences when matcher fails, part 1 ([#8448](https://github.com/facebook/jest/pull/8448)) - `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454)) +- `[*]` Manage the global timeout with `--timeout` command line argument. ([#8456](https://github.com/facebook/jest/pull/8456)) ### Fixes diff --git a/TestUtils.ts b/TestUtils.ts index ba80887bda29..1275e4ecb8eb 100644 --- a/TestUtils.ts +++ b/TestUtils.ts @@ -55,6 +55,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = { testNamePattern: '', testPathPattern: '', testResultsProcessor: null, + timeout: 5000, updateSnapshot: 'none', useStderr: false, verbose: false, diff --git a/docs/CLI.md b/docs/CLI.md index 2530a940879b..d6a7a7b664cd 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -301,6 +301,10 @@ Lets you specify a custom test runner. Lets you specify a custom test sequencer. Please refer to the documentation of the corresponding configuration property for details. +### `--timeout=` + +Default timeout of the test case. If the value is 0 then the timeout is 1 193 days. + ### `--updateSnapshot` Alias: `-u`. Use this flag to re-record every snapshot that fails during this test run. Can be used together with a test suite pattern or with `--testNamePattern` to re-record snapshots. diff --git a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap index 19ed2b0400be..2375a758ccc6 100644 --- a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap +++ b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap @@ -20,3 +20,37 @@ Snapshots: 0 total Time: <> Ran all test suites. `; + +exports[`exceeds the command line timeout 1`] = ` +Test Suites: 1 failed, 1 total +Tests: 1 failed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +`; + +exports[`does not exceed the command line timeout 1`] = ` +PASS __tests__/a-banana.js + ✓ banana +`; + +exports[`does not exceed the command line timeout 2`] = ` +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +`; + +exports[`if command line timeout=0 its mean no timeout 1`] = ` +PASS __tests__/a-banana.js + ✓ banana +`; + +exports[`if command line timeout=0 its mean no timeout 2`] = ` +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +`; diff --git a/e2e/__tests__/timeouts.test.ts b/e2e/__tests__/timeouts.test.ts index 09d38e6e9d1b..b5177166c475 100644 --- a/e2e/__tests__/timeouts.test.ts +++ b/e2e/__tests__/timeouts.test.ts @@ -60,3 +60,73 @@ test('does not exceed the timeout', () => { expect(wrap(summary)).toMatchSnapshot(); expect(status).toBe(0); }); + +test('exceeds the command line timeout', () => { + writeFiles(DIR, { + '__tests__/a-banana.js': ` + + test('banana', () => { + return new Promise(resolve => { + setTimeout(resolve, 1000); + }); + }); + `, + 'package.json': '{}', + }); + + const {stderr, status} = runJest(DIR, [ + '-w=1', + '--ci=false', + '--timeout=200', + ]); + const {rest, summary} = extractSummary(stderr); + expect(rest).toMatch( + /(jest\.setTimeout|jasmine\.DEFAULT_TIMEOUT_INTERVAL|Exceeded timeout)/, + ); + expect(wrap(summary)).toMatchSnapshot(); + expect(status).toBe(1); +}); + +test('does not exceed the command line timeout', () => { + writeFiles(DIR, { + '__tests__/a-banana.js': ` + + test('banana', () => { + return new Promise(resolve => { + setTimeout(resolve, 200); + }); + }); + `, + 'package.json': '{}', + }); + + const {stderr, status} = runJest(DIR, [ + '-w=1', + '--ci=false', + '--timeout=1000', + ]); + const {rest, summary} = extractSummary(stderr); + expect(wrap(rest)).toMatchSnapshot(); + expect(wrap(summary)).toMatchSnapshot(); + expect(status).toBe(0); +}); + +test('if command line timeout=0 its mean no timeout', () => { + writeFiles(DIR, { + '__tests__/a-banana.js': ` + + test('banana', () => { + return new Promise(resolve => { + setTimeout(resolve, 6000); + }); + }); + `, + 'package.json': '{}', + }); + + const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', '--timeout=0']); + const {rest, summary} = extractSummary(stderr); + expect(wrap(rest)).toMatchSnapshot(); + expect(wrap(summary)).toMatchSnapshot(); + expect(status).toBe(0); +}); diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index 2f7c38aa0279..ec48ec359f13 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -626,6 +626,12 @@ export const options = { description: 'This option sets the URL for the jsdom environment.', type: 'string' as 'string', }, + timeout: { + description: + 'This option sets the default timeouts of test cases.' + + "If you don't want timeout set it to 0", + type: 'number' as 'number', + }, timers: { description: 'Setting this value to fake allows the use of fake timers ' + diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index 97e22dc030da..b1e743606d30 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -114,6 +114,7 @@ const initialOptions: Config.InitialOptions = { testRunner: 'jasmine2', testSequencer: '@jest/test-sequencer', testURL: 'http://localhost', + timeout: 5000, timers: 'real', transform: { '^.+\\.js$': '/preprocessor.js', diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 88e557f1939e..6d721808e479 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -1562,3 +1562,19 @@ describe('displayName', () => { }, ); }); + +describe('timeout', () => { + it('should return timeout value if defined', () => { + console.warn.mockImplementation(() => {}); + const {options} = normalize({rootDir: '/root/', timeout: 1000}, {}); + + expect(options.timeout).toBe(1000); + expect(console.warn).not.toHaveBeenCalled(); + }); + + it('should return with 2^32 -1 if timeout=0', () => { + const {options} = normalize({rootDir: '/root/', timeout: 0}, {}); + + expect(options.timeout).toBe(Math.pow(2, 31) - 1); + }); +}); diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index 337e7ab47c81..9cf7161ef9ab 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -149,6 +149,7 @@ const groupOptions = ( testPathPattern: options.testPathPattern, testResultsProcessor: options.testResultsProcessor, testSequencer: options.testSequencer, + timeout: options.timeout, updateSnapshot: options.updateSnapshot, useStderr: options.useStderr, verbose: options.verbose, diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 813706ab60f2..f84424c49a9d 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -40,6 +40,7 @@ import VALID_CONFIG from './ValidConfig'; const ERROR = `${BULLET}Validation Error`; const PRESET_EXTENSIONS = ['.json', '.js']; const PRESET_NAME = 'jest-preset'; +const MAX_32_BIT_SIGNED_INTEGER = Math.pow(2, 31) - 1; type AllOptions = Config.ProjectConfig & Config.GlobalConfig; @@ -789,6 +790,11 @@ export default function normalize( value = oldOptions[key]; break; } + case 'timeout': { + value = + oldOptions[key] === 0 ? MAX_32_BIT_SIGNED_INTEGER : oldOptions[key]; + break; + } case 'automock': case 'browser': case 'cache': diff --git a/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap b/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap index fc4ae3ee76e0..b3aff5324245 100644 --- a/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap +++ b/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap @@ -112,6 +112,7 @@ exports[`prints the config object 1`] = ` "testNamePattern": "", "testPathPattern": "", "testResultsProcessor": null, + "timeout": 5000, "updateSnapshot": "none", "useStderr": false, "verbose": false, diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index 1860ad51bf96..25c8db594d34 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -32,6 +32,7 @@ async function jasmine2( const reporter = new JasmineReporter(globalConfig, config, testPath); const jasmineFactory = runtime.requireInternalModule(JASMINE); const jasmine = jasmineFactory.create({ + globalConfig, process, testPath, }); diff --git a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts index bde9103904fb..3b2ae0f88458 100644 --- a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts +++ b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts @@ -43,7 +43,7 @@ import Timer from './Timer'; const create = function(createOptions: Record): Jasmine { const j$ = {...createOptions} as Jasmine; - j$._DEFAULT_TIMEOUT_INTERVAL = 5000; + j$._DEFAULT_TIMEOUT_INTERVAL = createOptions.globalConfig.timeout || 5000; j$.getEnv = function(options?: object) { const env = (j$.currentEnv_ = j$.currentEnv_ || new j$.Env(options)); diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 0793c60d5b38..9dfc8db6a0a5 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -207,6 +207,7 @@ export type InitialOptions = { testRunner?: string; testSequencer?: string; testURL?: string; + timeout?: number; timers?: 'real' | 'fake'; transform?: { [key: string]: string; @@ -344,6 +345,7 @@ export type GlobalConfig = { testPathPattern: string; testResultsProcessor: string | null | undefined; testSequencer: string; + timeout: number; updateSnapshot: SnapshotUpdateState; useStderr: boolean; verbose: boolean | null | undefined; @@ -489,6 +491,7 @@ export type Argv = Arguments< testRunner: string; testSequencer: string; testURL: string; + timeout: number | null | undefined; timers: string; transform: string; transformIgnorePatterns: Array; From 8a7ac7b37c904c57cc37c9b23b58b9331254823e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ro=CC=81bert=20Kiss?= Date: Sun, 12 May 2019 20:45:14 +0200 Subject: [PATCH 2/8] feat: set testTimeout of jest-circus state in command line argument --- .../src/legacy-code-todo-rewrite/jestAdapterInit.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index 434dd63675f1..60b92c84015a 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -16,7 +16,12 @@ import { buildSnapshotResolver, } from 'jest-snapshot'; import throat from 'throat'; -import {addEventHandler, dispatch, ROOT_DESCRIBE_BLOCK_NAME} from '../state'; +import { + addEventHandler, + dispatch, + getState as getRunnerState, + ROOT_DESCRIBE_BLOCK_NAME, +} from '../state'; import {getTestID} from '../utils'; import run from '../run'; import globals from '..'; @@ -42,6 +47,8 @@ export const initialize = ({ testPath: Config.Path; parentProcess: Process; }) => { + if (globalConfig.timeout) getRunnerState().testTimeout = globalConfig.timeout; + const mutex = throat(globalConfig.maxConcurrency); Object.assign(global, globals); From 2ea81b9eb0b8bb5fd347760d6ec59db3b97fd701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Sun, 2 Jun 2019 12:07:24 +0200 Subject: [PATCH 3/8] refactor: rename timeout to testTimeout --- TestUtils.ts | 2 +- docs/CLI.md | 4 ++-- .../__snapshots__/timeouts.test.ts.snap | 10 +++++----- e2e/__tests__/timeouts.test.ts | 16 ++++++++++------ .../legacy-code-todo-rewrite/jestAdapterInit.ts | 3 ++- packages/jest-cli/src/cli/args.ts | 10 +++++----- packages/jest-config/src/ValidConfig.ts | 2 +- .../jest-config/src/__tests__/normalize.test.js | 10 +++++----- packages/jest-config/src/index.ts | 2 +- packages/jest-config/src/normalize.ts | 2 +- .../log_debug_messages.test.ts.snap | 2 +- .../jest-jasmine2/src/jasmine/jasmineLight.ts | 2 +- packages/jest-types/src/Config.ts | 6 +++--- 13 files changed, 38 insertions(+), 33 deletions(-) diff --git a/TestUtils.ts b/TestUtils.ts index 1275e4ecb8eb..7150270a7b2e 100644 --- a/TestUtils.ts +++ b/TestUtils.ts @@ -55,7 +55,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = { testNamePattern: '', testPathPattern: '', testResultsProcessor: null, - timeout: 5000, + testTimeout: 5000, updateSnapshot: 'none', useStderr: false, verbose: false, diff --git a/docs/CLI.md b/docs/CLI.md index d6a7a7b664cd..1be3b2af3316 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -301,9 +301,9 @@ Lets you specify a custom test runner. Lets you specify a custom test sequencer. Please refer to the documentation of the corresponding configuration property for details. -### `--timeout=` +### `--testTimeout=` -Default timeout of the test case. If the value is 0 then the timeout is 1 193 days. +Default timeout of a test. If the value is 0 then the timeout is ~1 193 days. ### `--updateSnapshot` diff --git a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap index 2375a758ccc6..609c578cbc0f 100644 --- a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap +++ b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap @@ -21,7 +21,7 @@ Time: <> Ran all test suites. `; -exports[`exceeds the command line timeout 1`] = ` +exports[`exceeds the command line testTimeout 1`] = ` Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total @@ -29,12 +29,12 @@ Time: <> Ran all test suites. `; -exports[`does not exceed the command line timeout 1`] = ` +exports[`does not exceed the command line testTimeout 1`] = ` PASS __tests__/a-banana.js ✓ banana `; -exports[`does not exceed the command line timeout 2`] = ` +exports[`does not exceed the command line testTimeout 2`] = ` Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total @@ -42,12 +42,12 @@ Time: <> Ran all test suites. `; -exports[`if command line timeout=0 its mean no timeout 1`] = ` +exports[`if command line timeout=0 its mean no testTimeout 1`] = ` PASS __tests__/a-banana.js ✓ banana `; -exports[`if command line timeout=0 its mean no timeout 2`] = ` +exports[`if command line timeout=0 its mean no testTimeout 2`] = ` Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total diff --git a/e2e/__tests__/timeouts.test.ts b/e2e/__tests__/timeouts.test.ts index b5177166c475..3430475e91aa 100644 --- a/e2e/__tests__/timeouts.test.ts +++ b/e2e/__tests__/timeouts.test.ts @@ -61,7 +61,7 @@ test('does not exceed the timeout', () => { expect(status).toBe(0); }); -test('exceeds the command line timeout', () => { +test('exceeds the command line testTimeout', () => { writeFiles(DIR, { '__tests__/a-banana.js': ` @@ -77,7 +77,7 @@ test('exceeds the command line timeout', () => { const {stderr, status} = runJest(DIR, [ '-w=1', '--ci=false', - '--timeout=200', + '--testTimeout=200', ]); const {rest, summary} = extractSummary(stderr); expect(rest).toMatch( @@ -87,7 +87,7 @@ test('exceeds the command line timeout', () => { expect(status).toBe(1); }); -test('does not exceed the command line timeout', () => { +test('does not exceed the command line testTimeout', () => { writeFiles(DIR, { '__tests__/a-banana.js': ` @@ -103,7 +103,7 @@ test('does not exceed the command line timeout', () => { const {stderr, status} = runJest(DIR, [ '-w=1', '--ci=false', - '--timeout=1000', + '--testTimeout=1000', ]); const {rest, summary} = extractSummary(stderr); expect(wrap(rest)).toMatchSnapshot(); @@ -111,7 +111,7 @@ test('does not exceed the command line timeout', () => { expect(status).toBe(0); }); -test('if command line timeout=0 its mean no timeout', () => { +test('if command line timeout=0 its mean no testTimeout', () => { writeFiles(DIR, { '__tests__/a-banana.js': ` @@ -124,7 +124,11 @@ test('if command line timeout=0 its mean no timeout', () => { 'package.json': '{}', }); - const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', '--timeout=0']); + const {stderr, status} = runJest(DIR, [ + '-w=1', + '--ci=false', + '--testTimeout=0', + ]); const {rest, summary} = extractSummary(stderr); expect(wrap(rest)).toMatchSnapshot(); expect(wrap(summary)).toMatchSnapshot(); diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index 60b92c84015a..10cfefcdf5a0 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -47,7 +47,8 @@ export const initialize = ({ testPath: Config.Path; parentProcess: Process; }) => { - if (globalConfig.timeout) getRunnerState().testTimeout = globalConfig.timeout; + if (globalConfig.testTimeout) + getRunnerState().testTimeout = globalConfig.testTimeout; const mutex = throat(globalConfig.maxConcurrency); diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index ec48ec359f13..44447a1000f0 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -622,16 +622,16 @@ export const options = { 'provided: `/path/to/testSequencer.js`', type: 'string' as 'string', }, - testURL: { - description: 'This option sets the URL for the jsdom environment.', - type: 'string' as 'string', - }, - timeout: { + testTimeout: { description: 'This option sets the default timeouts of test cases.' + "If you don't want timeout set it to 0", type: 'number' as 'number', }, + testURL: { + description: 'This option sets the URL for the jsdom environment.', + type: 'string' as 'string', + }, timers: { description: 'Setting this value to fake allows the use of fake timers ' + diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index b1e743606d30..ee69c72b178f 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -113,8 +113,8 @@ const initialOptions: Config.InitialOptions = { testResultsProcessor: 'processor-node-module', testRunner: 'jasmine2', testSequencer: '@jest/test-sequencer', + testTimeout: 5000, testURL: 'http://localhost', - timeout: 5000, timers: 'real', transform: { '^.+\\.js$': '/preprocessor.js', diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 6d721808e479..22e7d954c3be 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -1563,18 +1563,18 @@ describe('displayName', () => { ); }); -describe('timeout', () => { +describe('testTimeout', () => { it('should return timeout value if defined', () => { console.warn.mockImplementation(() => {}); - const {options} = normalize({rootDir: '/root/', timeout: 1000}, {}); + const {options} = normalize({rootDir: '/root/', testTimeout: 1000}, {}); - expect(options.timeout).toBe(1000); + expect(options.testTimeout).toBe(1000); expect(console.warn).not.toHaveBeenCalled(); }); it('should return with 2^32 -1 if timeout=0', () => { - const {options} = normalize({rootDir: '/root/', timeout: 0}, {}); + const {options} = normalize({rootDir: '/root/', testTimeout: 0}, {}); - expect(options.timeout).toBe(Math.pow(2, 31) - 1); + expect(options.testTimeout).toBe(Math.pow(2, 31) - 1); }); }); diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index 9cf7161ef9ab..9290126323df 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -149,7 +149,7 @@ const groupOptions = ( testPathPattern: options.testPathPattern, testResultsProcessor: options.testResultsProcessor, testSequencer: options.testSequencer, - timeout: options.timeout, + testTimeout: options.testTimeout, updateSnapshot: options.updateSnapshot, useStderr: options.useStderr, verbose: options.verbose, diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index f84424c49a9d..034f1eb4e0c6 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -790,7 +790,7 @@ export default function normalize( value = oldOptions[key]; break; } - case 'timeout': { + case 'testTimeout': { value = oldOptions[key] === 0 ? MAX_32_BIT_SIGNED_INTEGER : oldOptions[key]; break; diff --git a/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap b/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap index b3aff5324245..357612825f0a 100644 --- a/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap +++ b/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap @@ -112,7 +112,7 @@ exports[`prints the config object 1`] = ` "testNamePattern": "", "testPathPattern": "", "testResultsProcessor": null, - "timeout": 5000, + "testTimeout": 5000, "updateSnapshot": "none", "useStderr": false, "verbose": false, diff --git a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts index 3b2ae0f88458..2f9e08aa2362 100644 --- a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts +++ b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts @@ -43,7 +43,7 @@ import Timer from './Timer'; const create = function(createOptions: Record): Jasmine { const j$ = {...createOptions} as Jasmine; - j$._DEFAULT_TIMEOUT_INTERVAL = createOptions.globalConfig.timeout || 5000; + j$._DEFAULT_TIMEOUT_INTERVAL = createOptions.globalConfig.testTimeout || 5000; j$.getEnv = function(options?: object) { const env = (j$.currentEnv_ = j$.currentEnv_ || new j$.Env(options)); diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 9dfc8db6a0a5..00a78f0bd1ca 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -207,7 +207,7 @@ export type InitialOptions = { testRunner?: string; testSequencer?: string; testURL?: string; - timeout?: number; + testTimeout?: number; timers?: 'real' | 'fake'; transform?: { [key: string]: string; @@ -345,7 +345,7 @@ export type GlobalConfig = { testPathPattern: string; testResultsProcessor: string | null | undefined; testSequencer: string; - timeout: number; + testTimeout: number; updateSnapshot: SnapshotUpdateState; useStderr: boolean; verbose: boolean | null | undefined; @@ -491,7 +491,7 @@ export type Argv = Arguments< testRunner: string; testSequencer: string; testURL: string; - timeout: number | null | undefined; + testTimeout: number | null | undefined; timers: string; transform: string; transformIgnorePatterns: Array; From 635ddc0c6b3f91153739835abf5cd1e93cc4f575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Sun, 2 Jun 2019 12:14:47 +0200 Subject: [PATCH 4/8] doc: fix changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f65d3dd02a69..80a3ad1707b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - `[expect]` Highlight substring differences when matcher fails, part 1 ([#8448](https://github.com/facebook/jest/pull/8448)) - `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454)) -- `[*]` Manage the global timeout with `--timeout` command line argument. ([#8456](https://github.com/facebook/jest/pull/8456)) +- `[*]` Manage the global timeout with `--testTimeout` command line argument. ([#8456](https://github.com/facebook/jest/pull/8456)) ### Fixes From 76f120a7d2f53a90a0a093d828fb6d047167d3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Tue, 4 Jun 2019 22:15:49 +0200 Subject: [PATCH 5/8] feat: remove 0 special meaning and trow error when timeout is negative --- docs/CLI.md | 2 +- .../src/__tests__/__snapshots__/normalize.test.js.snap | 10 ++++++++++ packages/jest-config/src/__tests__/normalize.test.js | 8 ++++---- packages/jest-config/src/normalize.ts | 10 +++++++--- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index 1be3b2af3316..2aee9c6d5b9c 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -303,7 +303,7 @@ Lets you specify a custom test sequencer. Please refer to the documentation of t ### `--testTimeout=` -Default timeout of a test. If the value is 0 then the timeout is ~1 193 days. +Default timeout of a test in milliseconds. ### `--updateSnapshot` diff --git a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap index b566ee92df51..96d7622c963e 100644 --- a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap +++ b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap @@ -168,6 +168,16 @@ exports[`testPathPattern ignores invalid regular expressions exports[`testPathPattern --testPathPattern ignores invalid regular expressions and logs a warning 1`] = `" Invalid testPattern a( supplied. Running all tests instead."`; +exports[`testTimeout should throw an error if timeout is a negative number 1`] = ` +"Validation Error: + + Option \\"testTimeout\\" must be a natural number. + + Configuration Documentation: + https://jestjs.io/docs/configuration.html +" +`; + exports[`watchPlugins throw error when a watch plugin is not found 1`] = ` "Validation Error: diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 22e7d954c3be..3930cd473e09 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -1572,9 +1572,9 @@ describe('testTimeout', () => { expect(console.warn).not.toHaveBeenCalled(); }); - it('should return with 2^32 -1 if timeout=0', () => { - const {options} = normalize({rootDir: '/root/', testTimeout: 0}, {}); - - expect(options.testTimeout).toBe(Math.pow(2, 31) - 1); + it('should throw an error if timeout is a negative number', () => { + expect(() => + normalize({rootDir: '/root/', testTimeout: -1}, {}), + ).toThrowErrorMatchingSnapshot(); }); }); diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 034f1eb4e0c6..d31047ac538d 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -40,7 +40,6 @@ import VALID_CONFIG from './ValidConfig'; const ERROR = `${BULLET}Validation Error`; const PRESET_EXTENSIONS = ['.json', '.js']; const PRESET_NAME = 'jest-preset'; -const MAX_32_BIT_SIGNED_INTEGER = Math.pow(2, 31) - 1; type AllOptions = Config.ProjectConfig & Config.GlobalConfig; @@ -791,8 +790,13 @@ export default function normalize( break; } case 'testTimeout': { - value = - oldOptions[key] === 0 ? MAX_32_BIT_SIGNED_INTEGER : oldOptions[key]; + if (oldOptions[key] < 0) { + throw createConfigError( + ` Option "${chalk.bold('testTimeout')}" must be a natural number.`, + ); + } + + value = oldOptions[key]; break; } case 'automock': From ef4026a92196fb4ff4cf74ca1ceccbff4ae8ecf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Tue, 4 Jun 2019 23:01:46 +0200 Subject: [PATCH 6/8] test: remove invalid e2e test --- .../__snapshots__/timeouts.test.ts.snap | 31 ++++++++----------- e2e/__tests__/timeouts.test.ts | 24 -------------- 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap index 609c578cbc0f..007ed3bf333f 100644 --- a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap +++ b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap @@ -1,11 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`does not exceed the timeout 1`] = ` +exports[`does not exceed the command line testTimeout 1`] = ` PASS __tests__/a-banana.js ✓ banana `; -exports[`does not exceed the timeout 2`] = ` +exports[`does not exceed the command line testTimeout 2`] = ` Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total @@ -13,9 +13,14 @@ Time: <> Ran all test suites. `; -exports[`exceeds the timeout 1`] = ` -Test Suites: 1 failed, 1 total -Tests: 1 failed, 1 total +exports[`does not exceed the timeout 1`] = ` +PASS __tests__/a-banana.js + ✓ banana +`; + +exports[`does not exceed the timeout 2`] = ` +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total Snapshots: 0 total Time: <> Ran all test suites. @@ -29,24 +34,14 @@ Time: <> Ran all test suites. `; -exports[`does not exceed the command line testTimeout 1`] = ` -PASS __tests__/a-banana.js - ✓ banana -`; - -exports[`does not exceed the command line testTimeout 2`] = ` -Test Suites: 1 passed, 1 total -Tests: 1 passed, 1 total +exports[`exceeds the timeout 1`] = ` +Test Suites: 1 failed, 1 total +Tests: 1 failed, 1 total Snapshots: 0 total Time: <> Ran all test suites. `; -exports[`if command line timeout=0 its mean no testTimeout 1`] = ` -PASS __tests__/a-banana.js - ✓ banana -`; - exports[`if command line timeout=0 its mean no testTimeout 2`] = ` Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total diff --git a/e2e/__tests__/timeouts.test.ts b/e2e/__tests__/timeouts.test.ts index 3430475e91aa..7e9ab43e5da1 100644 --- a/e2e/__tests__/timeouts.test.ts +++ b/e2e/__tests__/timeouts.test.ts @@ -110,27 +110,3 @@ test('does not exceed the command line testTimeout', () => { expect(wrap(summary)).toMatchSnapshot(); expect(status).toBe(0); }); - -test('if command line timeout=0 its mean no testTimeout', () => { - writeFiles(DIR, { - '__tests__/a-banana.js': ` - - test('banana', () => { - return new Promise(resolve => { - setTimeout(resolve, 6000); - }); - }); - `, - 'package.json': '{}', - }); - - const {stderr, status} = runJest(DIR, [ - '-w=1', - '--ci=false', - '--testTimeout=0', - ]); - const {rest, summary} = extractSummary(stderr); - expect(wrap(rest)).toMatchSnapshot(); - expect(wrap(summary)).toMatchSnapshot(); - expect(status).toBe(0); -}); From 9bd646d19b5bea5e217bccd46d4518534894beb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Tue, 4 Jun 2019 23:22:33 +0200 Subject: [PATCH 7/8] test: remove invalid e2e test --- e2e/__tests__/__snapshots__/timeouts.test.ts.snap | 8 -------- 1 file changed, 8 deletions(-) diff --git a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap index 007ed3bf333f..4a2e6ad2faba 100644 --- a/e2e/__tests__/__snapshots__/timeouts.test.ts.snap +++ b/e2e/__tests__/__snapshots__/timeouts.test.ts.snap @@ -41,11 +41,3 @@ Snapshots: 0 total Time: <> Ran all test suites. `; - -exports[`if command line timeout=0 its mean no testTimeout 2`] = ` -Test Suites: 1 passed, 1 total -Tests: 1 passed, 1 total -Snapshots: 0 total -Time: <> -Ran all test suites. -`; From bf4069289d2161b31eec957c16627d5d1170d5e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Wed, 5 Jun 2019 20:57:39 +0200 Subject: [PATCH 8/8] fix: polishes --- docs/CLI.md | 2 +- .../src/legacy-code-todo-rewrite/jestAdapterInit.ts | 3 ++- packages/jest-cli/src/cli/args.ts | 4 +--- packages/jest-jasmine2/src/index.ts | 2 +- packages/jest-jasmine2/src/jasmine/jasmineLight.ts | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index 2aee9c6d5b9c..079524e58d13 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -303,7 +303,7 @@ Lets you specify a custom test sequencer. Please refer to the documentation of t ### `--testTimeout=` -Default timeout of a test in milliseconds. +Default timeout of a test in milliseconds. Default value: 5000. ### `--updateSnapshot` diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts index 10cfefcdf5a0..8f75aba0df63 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts @@ -47,8 +47,9 @@ export const initialize = ({ testPath: Config.Path; parentProcess: Process; }) => { - if (globalConfig.testTimeout) + if (globalConfig.testTimeout) { getRunnerState().testTimeout = globalConfig.testTimeout; + } const mutex = throat(globalConfig.maxConcurrency); diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index 44447a1000f0..7bba9a038621 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -623,9 +623,7 @@ export const options = { type: 'string' as 'string', }, testTimeout: { - description: - 'This option sets the default timeouts of test cases.' + - "If you don't want timeout set it to 0", + description: 'This option sets the default timeouts of test cases.', type: 'number' as 'number', }, testURL: { diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index 25c8db594d34..7c5e887ed88d 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -32,9 +32,9 @@ async function jasmine2( const reporter = new JasmineReporter(globalConfig, config, testPath); const jasmineFactory = runtime.requireInternalModule(JASMINE); const jasmine = jasmineFactory.create({ - globalConfig, process, testPath, + testTimeout: globalConfig.testTimeout, }); const env = jasmine.getEnv(); diff --git a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts index 2f9e08aa2362..837f04906805 100644 --- a/packages/jest-jasmine2/src/jasmine/jasmineLight.ts +++ b/packages/jest-jasmine2/src/jasmine/jasmineLight.ts @@ -43,7 +43,7 @@ import Timer from './Timer'; const create = function(createOptions: Record): Jasmine { const j$ = {...createOptions} as Jasmine; - j$._DEFAULT_TIMEOUT_INTERVAL = createOptions.globalConfig.testTimeout || 5000; + j$._DEFAULT_TIMEOUT_INTERVAL = createOptions.testTimeout || 5000; j$.getEnv = function(options?: object) { const env = (j$.currentEnv_ = j$.currentEnv_ || new j$.Env(options));