Skip to content

Commit

Permalink
feat(test-sequencer): Exposes globalConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Tyrrell22 committed Sep 16, 2023
1 parent 81cc9f0 commit cfc5f07
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
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);
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

0 comments on commit cfc5f07

Please sign in to comment.