From cfc5f072752591ef3efdbf7f0e3fb536d905b08f Mon Sep 17 00:00:00 2001 From: Eric tyrrell Date: Sat, 16 Sep 2023 13:50:00 -0400 Subject: [PATCH] feat(test-sequencer): Exposes globalConfig --- e2e/__tests__/customTestSequencers.test.ts | 53 +++++++++++++++++++ .../testSequencerWithSeed.js | 19 +++++++ packages/jest-core/src/runJest.ts | 2 +- packages/jest-test-sequencer/src/index.ts | 3 ++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 e2e/custom-test-sequencer/testSequencerWithSeed.js diff --git a/e2e/__tests__/customTestSequencers.test.ts b/e2e/__tests__/customTestSequencers.test.ts index 40f0c43fc3cf..7feb8d1e6c13 100644 --- a/e2e/__tests__/customTestSequencers.test.ts +++ b/e2e/__tests__/customTestSequencers.test.ts @@ -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: '/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: '/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', + ]); +}); diff --git a/e2e/custom-test-sequencer/testSequencerWithSeed.js b/e2e/custom-test-sequencer/testSequencerWithSeed.js new file mode 100644 index 000000000000..43f8b86a39a3 --- /dev/null +++ b/e2e/custom-test-sequencer/testSequencerWithSeed.js @@ -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; diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index 45b31137d292..5b097ecd4d4d 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -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 = []; if (changedFilesPromise && globalConfig.watch) { diff --git a/packages/jest-test-sequencer/src/index.ts b/packages/jest-test-sequencer/src/index.ts index 0a8239e8ce63..2946df37fe7c 100644 --- a/packages/jest-test-sequencer/src/index.ts +++ b/packages/jest-test-sequencer/src/index.ts @@ -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; @@ -46,6 +47,8 @@ type ShardPositionOptions = ShardOptions & { export default class TestSequencer { private readonly _cache = new Map(); + constructor(protected readonly globalConfig: Config.GlobalConfig) {} + _getCachePath(testContext: TestContext): string { const {config} = testContext; const HasteMapClass = HasteMap.getStatic(config);