Skip to content

Commit

Permalink
feat: set global timeout in command line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ert78gb committed May 12, 2019
1 parent 15e2cc6 commit 335d649
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 1 deletion.
1 change: 1 addition & 0 deletions TestUtils.ts
Expand Up @@ -55,6 +55,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
testNamePattern: '',
testPathPattern: '',
testResultsProcessor: null,
timeout: 5000,
updateSnapshot: 'none',
useStderr: false,
verbose: false,
Expand Down
4 changes: 4 additions & 0 deletions docs/CLI.md
Expand Up @@ -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=<number>`

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.
Expand Down
34 changes: 34 additions & 0 deletions e2e/__tests__/__snapshots__/timeouts.test.ts.snap
Expand Up @@ -20,3 +20,37 @@ Snapshots: 0 total
Time: <<REPLACED>>
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: <<REPLACED>>
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: <<REPLACED>>
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: <<REPLACED>>
Ran all test suites.
`;
70 changes: 70 additions & 0 deletions e2e/__tests__/timeouts.test.ts
Expand Up @@ -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);
});
6 changes: 6 additions & 0 deletions packages/jest-cli/src/cli/args.ts
Expand Up @@ -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 ' +
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/ValidConfig.ts
Expand Up @@ -114,6 +114,7 @@ const initialOptions: Config.InitialOptions = {
testRunner: 'jasmine2',
testSequencer: '@jest/test-sequencer',
testURL: 'http://localhost',
timeout: 5000,
timers: 'real',
transform: {
'^.+\\.js$': '<rootDir>/preprocessor.js',
Expand Down
16 changes: 16 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions packages/jest-config/src/index.ts
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -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;

Expand Down Expand Up @@ -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':
Expand Down
Expand Up @@ -112,6 +112,7 @@ exports[`prints the config object 1`] = `
"testNamePattern": "",
"testPathPattern": "",
"testResultsProcessor": null,
"timeout": 5000,
"updateSnapshot": "none",
"useStderr": false,
"verbose": false,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-jasmine2/src/index.ts
Expand Up @@ -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,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jasmine/jasmineLight.ts
Expand Up @@ -43,7 +43,7 @@ import Timer from './Timer';
const create = function(createOptions: Record<string, any>): 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));
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-types/src/Config.ts
Expand Up @@ -207,6 +207,7 @@ export type InitialOptions = {
testRunner?: string;
testSequencer?: string;
testURL?: string;
timeout?: number;
timers?: 'real' | 'fake';
transform?: {
[key: string]: string;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -489,6 +491,7 @@ export type Argv = Arguments<
testRunner: string;
testSequencer: string;
testURL: string;
timeout: number | null | undefined;
timers: string;
transform: string;
transformIgnorePatterns: Array<string>;
Expand Down

0 comments on commit 335d649

Please sign in to comment.