Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposes globalConfig to custom test sequencers #14535

Merged
merged 11 commits into from
Sep 19, 2023
53 changes: 53 additions & 0 deletions e2e/__tests__/customTestSequencers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,56 @@ test('run failed tests async', () => {
.split('\n');
expect(sequence).toEqual(['./c.test.js', './d.test.js']);
});

test('run tests based on even seed', () => {
const result = runJest(
dir,
[
'-i',
'--config',
JSON.stringify({
testSequencer: '<rootDir>/testSequencerWithSeed.js',
}),
'--seed=2',
],
{},
);
console.log({result});
expect(result.exitCode).toBe(0);
const sequence = extractSummary(result.stderr)
.rest.replace(/PASS /g, '')
.split('\n');
expect(sequence).toEqual([
'./a.test.js',
'./b.test.js',
'./c.test.js',
'./d.test.js',
'./e.test.js',
]);
});

test('run tests based on odd seed', () => {
const result = runJest(
dir,
[
'-i',
'--config',
JSON.stringify({
testSequencer: '<rootDir>/testSequencerWithSeed.js',
}),
'--seed=1',
],
{},
);
expect(result.exitCode).toBe(0);
const sequence = extractSummary(result.stderr)
.rest.replace(/PASS /g, '')
.split('\n');
expect(sequence).toEqual([
'./e.test.js',
'./d.test.js',
'./c.test.js',
'./b.test.js',
'./a.test.js',
]);
});
19 changes: 19 additions & 0 deletions e2e/custom-test-sequencer/testSequencerWithSeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Sequencer = require('@jest/test-sequencer').default;

class CustomSequencer extends Sequencer {
sort(tests) {
const copyTests = Array.from(tests);
const seed = this.globalConfig.seed;
const sortedTests = copyTests.sort((testA, testB) =>
testA.path > testB.path ? 1 : -1,
);

if (seed % 2 === 0) {
return sortedTests;
} else {
return sortedTests.reverse();
}
}
}

module.exports = CustomSequencer;
2 changes: 1 addition & 1 deletion packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export default async function runJest({
const Sequencer: typeof TestSequencer = await requireOrImportModule(
globalConfig.testSequencer,
);
const sequencer = new Sequencer();
const sequencer = new Sequencer(globalConfig);
SimenB marked this conversation as resolved.
Show resolved Hide resolved
let allTests: Array<Test> = [];

if (changedFilesPromise && globalConfig.watch) {
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-test-sequencer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as path from 'path';
import * as fs from 'graceful-fs';
import slash = require('slash');
import type {AggregatedResult, Test, TestContext} from '@jest/test-result';
import type {Config} from '@jest/types';
import HasteMap from 'jest-haste-map';

const FAIL = 0;
Expand Down Expand Up @@ -46,6 +47,8 @@ type ShardPositionOptions = ShardOptions & {
export default class TestSequencer {
private readonly _cache = new Map<TestContext, Cache>();

constructor(protected readonly globalConfig: Config.GlobalConfig) {}

_getCachePath(testContext: TestContext): string {
const {config} = testContext;
const HasteMapClass = HasteMap.getStatic(config);
Expand Down