Skip to content

Commit cbe0ac1

Browse files
authored
jest-circus runs children in shuffled order (#12922)
1 parent ab510f5 commit cbe0ac1

33 files changed

+889
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
- `[jest-changed-files]` Support Sapling ([#13941](https://github.com/facebook/jest/pull/13941))
6+
- `[jest-circus, @jest/cli, jest-config]` Add feature to randomize order of tests via CLI flag or through the config file([#12922](https://github.com/facebook/jest/pull/12922))
67
- `[jest-cli, jest-config, @jest/core, jest-haste-map, @jest/reporters, jest-runner, jest-runtime, @jest/types]` Add `workerThreads` configuration option to allow using [worker threads](https://nodejs.org/dist/latest/docs/api/worker_threads.html) for parallelization ([#13939](https://github.com/facebook/jest/pull/13939))
78
- `[jest-config]` Add `openHandlesTimeout` option to configure possible open handles warning. ([#13875](https://github.com/facebook/jest/pull/13875))
89
- `[@jest/create-cache-key-function]` Allow passing `length` argument to `createCacheKey()` function and set its default value to `16` on Windows ([#13827](https://github.com/facebook/jest/pull/13827))

docs/CLI.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,22 @@ If configuration files are found in the specified paths, _all_ projects specifie
332332

333333
:::
334334

335+
### `--randomize`
336+
337+
Shuffle the order of the tests within a file. The shuffling is based on the seed. See [`--seed=<num>`](#--seednum) for more info.
338+
339+
Seed value is displayed when this option is set. Equivalent to setting the CLI option [`--showSeed`](#--showseed).
340+
341+
```bash
342+
jest --randomize --seed 1234
343+
```
344+
345+
:::note
346+
347+
This option is only supported using the default `jest-circus` test runner.
348+
349+
:::
350+
335351
### `--reporters`
336352

337353
Run tests with specified reporters. [Reporter options](configuration#reporters-arraymodulename--modulename-options) are not available via CLI. Example with multiple reporters:

docs/Configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,12 @@ With the `projects` option enabled, Jest will copy the root-level configuration
12221222

12231223
:::
12241224

1225+
### `randomize` \[boolean]
1226+
1227+
Default: `false`
1228+
1229+
The equivalent of the [`--randomize`](CLI.md#--randomize) flag to randomize the order of the tests in a file.
1230+
12251231
### `reporters` \[array&lt;moduleName | \[moduleName, options]&gt;]
12261232

12271233
Default: `undefined`
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`works with each 1`] = `
4+
" ✓ test1
5+
✓ test2
6+
✓ test3
7+
describe2
8+
✓ test4
9+
✓ test6
10+
✓ test5
11+
describe1
12+
✓ test4
13+
✓ test6
14+
✓ test5
15+
describe3
16+
✓ test11
17+
✓ test12
18+
✓ test10
19+
describe4
20+
✓ test14
21+
✓ test15
22+
✓ test13"
23+
`;
24+
25+
exports[`works with hooks 1`] = `
26+
" ✓ test1
27+
✓ test2
28+
✓ test3
29+
describe2
30+
✓ test7
31+
✓ test9
32+
✓ test8
33+
describe1
34+
✓ test4
35+
✓ test6
36+
✓ test5
37+
describe3
38+
✓ test11
39+
✓ test12
40+
✓ test10
41+
describe4
42+
✓ test14
43+
✓ test15
44+
✓ test13"
45+
`;
46+
47+
exports[`works with passing tests 1`] = `
48+
" ✓ test1
49+
✓ test2
50+
✓ test3
51+
describe2
52+
✓ test7
53+
✓ test9
54+
✓ test8
55+
describe1
56+
✓ test4
57+
✓ test6
58+
✓ test5
59+
describe3
60+
✓ test11
61+
✓ test12
62+
✓ test10
63+
describe4
64+
✓ test14
65+
✓ test15
66+
✓ test13"
67+
`;
68+
69+
exports[`works with snapshots 1`] = `
70+
" ✓ test1
71+
✓ test2
72+
✓ test3
73+
describe2
74+
✓ test4
75+
✓ test6
76+
✓ test5
77+
describe1
78+
✓ test4
79+
✓ test6
80+
✓ test5
81+
describe3
82+
✓ test11
83+
✓ test12
84+
✓ test10
85+
describe4
86+
✓ test14
87+
✓ test15
88+
✓ test13"
89+
`;

e2e/__tests__/randomize.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as path from 'path';
9+
import {skipSuiteOnJasmine} from '@jest/test-utils';
10+
import {extractSummary} from '../Utils';
11+
import runJest, {RunJestResult} from '../runJest';
12+
13+
skipSuiteOnJasmine();
14+
15+
const dir = path.resolve(__dirname, '../randomize');
16+
17+
const trimFirstLine = (str: string): string =>
18+
str.split('\n').slice(1).join('\n');
19+
20+
function runJestTwice(
21+
dir: string,
22+
args: Array<string>,
23+
): [RunJestResult, RunJestResult] {
24+
return [
25+
runJest(dir, [...args, '--randomize']),
26+
runJest(dir, [...args, '--config', 'different-config.json']),
27+
];
28+
}
29+
30+
test('works with passing tests', () => {
31+
const [result1, result2] = runJestTwice(dir, [
32+
'success.test.js',
33+
'--seed',
34+
'123',
35+
]);
36+
37+
const rest1 = trimFirstLine(extractSummary(result1.stderr).rest);
38+
const rest2 = trimFirstLine(extractSummary(result2.stderr).rest);
39+
40+
expect(rest1).toEqual(rest2);
41+
expect(rest1).toMatchSnapshot();
42+
});
43+
44+
test('works with each', () => {
45+
const [result1, result2] = runJestTwice(dir, [
46+
'each.test.js',
47+
'--seed',
48+
'123',
49+
]);
50+
51+
const rest1 = trimFirstLine(extractSummary(result1.stderr).rest);
52+
const rest2 = trimFirstLine(extractSummary(result2.stderr).rest);
53+
54+
expect(rest1).toEqual(rest2);
55+
expect(rest1).toMatchSnapshot();
56+
});
57+
58+
test('works with hooks', () => {
59+
const [result1, result2] = runJestTwice(dir, [
60+
'hooks.test.js',
61+
'--seed',
62+
'123',
63+
]);
64+
65+
// Change in formatting could change this one
66+
const rest1 = trimFirstLine(extractSummary(result1.stderr).rest);
67+
const rest2 = trimFirstLine(extractSummary(result2.stderr).rest);
68+
69+
expect(rest1).toEqual(rest2);
70+
expect(rest1).toMatchSnapshot();
71+
});
72+
73+
test('works with snapshots', () => {
74+
const [result1, result2] = runJestTwice(dir, [
75+
'snapshots.test.js',
76+
'--seed',
77+
'123',
78+
]);
79+
80+
const rest1 = trimFirstLine(extractSummary(result1.stderr).rest);
81+
const rest2 = trimFirstLine(extractSummary(result2.stderr).rest);
82+
83+
expect(rest1).toEqual(rest2);
84+
expect(rest1).toMatchSnapshot();
85+
});

e2e/__tests__/showSeed.test.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,41 @@ const dir = path.resolve(__dirname, '../jest-object');
1414
const randomSeedValueRegExp = /Seed:\s+<<REPLACED>>/;
1515
const seedValueRegExp = /Seed:\s+1234/;
1616

17-
test('--showSeed changes report to output seed', () => {
18-
const {stderr} = runJest(dir, ['--showSeed', '--no-cache']);
17+
describe.each(['showSeed', 'randomize'])('Option %s', option => {
18+
test(`--${option} changes report to output seed`, () => {
19+
const {stderr} = runJest(dir, [`--${option}`, '--no-cache']);
1920

20-
const {summary} = extractSummary(stderr);
21+
const {summary} = extractSummary(stderr);
2122

22-
expect(replaceSeed(summary)).toMatch(randomSeedValueRegExp);
23-
});
23+
expect(replaceSeed(summary)).toMatch(randomSeedValueRegExp);
24+
});
2425

25-
test('if --showSeed is not present the report will not show the seed', () => {
26-
const {stderr} = runJest(dir, ['--seed', '1234']);
26+
test(`if --${option} is not present the report will not show the seed`, () => {
27+
const {stderr} = runJest(dir, ['--seed', '1234']);
2728

28-
const {summary} = extractSummary(stderr);
29+
const {summary} = extractSummary(stderr);
2930

30-
expect(replaceSeed(summary)).not.toMatch(randomSeedValueRegExp);
31-
});
31+
expect(replaceSeed(summary)).not.toMatch(randomSeedValueRegExp);
32+
});
3233

33-
test('if showSeed is present in the config the report will show the seed', () => {
34-
const {stderr} = runJest(dir, [
35-
'--seed',
36-
'1234',
37-
'--config',
38-
'different-config.json',
39-
]);
34+
test(`if ${option} is present in the config the report will show the seed`, () => {
35+
const {stderr} = runJest(dir, [
36+
'--seed',
37+
'1234',
38+
'--config',
39+
`${option}-config.json`,
40+
]);
4041

41-
const {summary} = extractSummary(stderr);
42+
const {summary} = extractSummary(stderr);
4243

43-
expect(summary).toMatch(seedValueRegExp);
44-
});
44+
expect(summary).toMatch(seedValueRegExp);
45+
});
4546

46-
test('--seed --showSeed will show the seed in the report', () => {
47-
const {stderr} = runJest(dir, ['--showSeed', '--seed', '1234']);
47+
test(`--seed --${option} will show the seed in the report`, () => {
48+
const {stderr} = runJest(dir, [`--${option}`, '--seed', '1234']);
4849

49-
const {summary} = extractSummary(stderr);
50+
const {summary} = extractSummary(stderr);
5051

51-
expect(summary).toMatch(seedValueRegExp);
52+
expect(summary).toMatch(seedValueRegExp);
53+
});
5254
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"displayName": "Config from randomize-config.json file",
3+
"randomize": true
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"displayName": "Config from showSeed-config.json file",
3+
"showSeed": true
4+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`describe1 test4 1`] = `4`;
4+
5+
exports[`describe1 test5 1`] = `5`;
6+
7+
exports[`describe1 test6 1`] = `6`;
8+
9+
exports[`describe2 test4 1`] = `4`;
10+
11+
exports[`describe2 test5 1`] = `5`;
12+
13+
exports[`describe2 test6 1`] = `6`;
14+
15+
exports[`describe3 describe4 test13 1`] = `13`;
16+
17+
exports[`describe3 describe4 test14 1`] = `14`;
18+
19+
exports[`describe3 describe4 test15 1`] = `15`;
20+
21+
exports[`describe3 test10 1`] = `10`;
22+
23+
exports[`describe3 test11 1`] = `11`;
24+
25+
exports[`describe3 test12 1`] = `12`;
26+
27+
exports[`test1 1`] = `1`;
28+
29+
exports[`test2 1`] = `2`;
30+
31+
exports[`test3 1`] = `3`;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
it.each([1, 2, 3])('test%d', () => {
9+
expect(true).toBe(true);
10+
});
11+
12+
describe.each([1, 2])('describe%d', () => {
13+
it.each([4, 5, 6])('test%d', () => {
14+
expect(true).toBe(true);
15+
});
16+
});
17+
18+
describe('describe3', () => {
19+
it.each([10, 11, 12])('test%d', () => {
20+
expect(true).toBe(true);
21+
});
22+
23+
describe('describe4', () => {
24+
it.each([13, 14, 15])('test%d', () => {
25+
expect(true).toBe(true);
26+
});
27+
});
28+
});

0 commit comments

Comments
 (0)