Skip to content

Commit 76a47d8

Browse files
Exposes globalConfig to custom test sequencers (#14535)
1 parent 0a45938 commit 76a47d8

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
- `[jest-test-sequencer, jest-core]` Exposes globalConfig & contexts to TestSequencer ([#14535](https://github.com/jestjs/jest/pull/14535))
6+
57
### Fixes
68

79
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))

e2e/__tests__/customTestSequencers.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,55 @@ test('run failed tests async', () => {
7979
.split('\n');
8080
expect(sequence).toEqual(['./c.test.js', './d.test.js']);
8181
});
82+
83+
test('run tests based on even seed', () => {
84+
const result = runJest(
85+
dir,
86+
[
87+
'-i',
88+
'--config',
89+
JSON.stringify({
90+
testSequencer: '<rootDir>/testSequencerWithSeed.js',
91+
}),
92+
'--seed=2',
93+
],
94+
{},
95+
);
96+
expect(result.exitCode).toBe(0);
97+
const sequence = extractSummary(result.stderr)
98+
.rest.replace(/PASS /g, '')
99+
.split('\n');
100+
expect(sequence).toEqual([
101+
'./a.test.js',
102+
'./b.test.js',
103+
'./c.test.js',
104+
'./d.test.js',
105+
'./e.test.js',
106+
]);
107+
});
108+
109+
test('run tests based on odd seed', () => {
110+
const result = runJest(
111+
dir,
112+
[
113+
'-i',
114+
'--config',
115+
JSON.stringify({
116+
testSequencer: '<rootDir>/testSequencerWithSeed.js',
117+
}),
118+
'--seed=1',
119+
],
120+
{},
121+
);
122+
expect(result.exitCode).toBe(0);
123+
const sequence = extractSummary(result.stderr)
124+
.rest.replace(/PASS /g, '')
125+
.split('\n');
126+
expect(sequence).toEqual([
127+
'./e.test.js',
128+
'./d.test.js',
129+
'./c.test.js',
130+
'./b.test.js',
131+
'./a.test.js',
132+
]);
133+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
const Sequencer = require('@jest/test-sequencer').default;
9+
10+
class CustomSequencer extends Sequencer {
11+
constructor(_options) {
12+
super(_options);
13+
this.globalConfig = _options.globalConfig;
14+
}
15+
16+
sort(tests) {
17+
const copyTests = Array.from(tests);
18+
const seed = this.globalConfig.seed;
19+
const sortedTests = copyTests.sort((testA, testB) =>
20+
testA.path > testB.path ? 1 : -1,
21+
);
22+
23+
if (seed % 2 === 0) {
24+
return sortedTests;
25+
} else {
26+
return sortedTests.reverse();
27+
}
28+
}
29+
}
30+
31+
module.exports = CustomSequencer;

packages/jest-core/src/runJest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export default async function runJest({
159159
const Sequencer: typeof TestSequencer = await requireOrImportModule(
160160
globalConfig.testSequencer,
161161
);
162-
const sequencer = new Sequencer();
162+
const sequencer = new Sequencer({contexts, globalConfig});
163163
let allTests: Array<Test> = [];
164164

165165
if (changedFilesPromise && globalConfig.watch) {

packages/jest-test-sequencer/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ import * as path from 'path';
1010
import * as fs from 'graceful-fs';
1111
import slash = require('slash');
1212
import type {AggregatedResult, Test, TestContext} from '@jest/test-result';
13+
import type {Config} from '@jest/types';
1314
import HasteMap from 'jest-haste-map';
1415

1516
const FAIL = 0;
1617
const SUCCESS = 1;
1718

19+
export type TestSequencerOptions = {
20+
contexts: ReadonlyArray<TestContext>;
21+
globalConfig: Config.GlobalConfig;
22+
};
23+
1824
type Cache = {
1925
[key: string]:
2026
| [testStatus: typeof FAIL | typeof SUCCESS, testDuration: number]
@@ -46,6 +52,9 @@ type ShardPositionOptions = ShardOptions & {
4652
export default class TestSequencer {
4753
private readonly _cache = new Map<TestContext, Cache>();
4854

55+
// eslint-disable-next-line @typescript-eslint/no-empty-function
56+
constructor(_options?: TestSequencerOptions) {}
57+
4958
_getCachePath(testContext: TestContext): string {
5059
const {config} = testContext;
5160
const HasteMapClass = HasteMap.getStatic(config);

0 commit comments

Comments
 (0)