Skip to content

Commit 3aa3045

Browse files
authored
fix: sort test files before the seeded shuffle (#11926)
1 parent a0dfc8b commit 3aa3045

2 files changed

Lines changed: 61 additions & 10 deletions

File tree

‎packages/vitest/src/node/sequencers/RandomSequencer.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ export class RandomSequencer extends BaseSequencer {
66
public async sort(files: TestSpecification[]): Promise<TestSpecification[]> {
77
const { sequence } = this.ctx.config
88

9-
return shuffle(files, sequence.seed)
9+
// the glob order can change between runs, so only the seed may decide the order
10+
const sorted = [...files].sort(compareSpecifications)
11+
return shuffle(sorted, sequence.seed)
1012
}
1113
}
14+
15+
function compareSpecifications(a: TestSpecification, b: TestSpecification): number {
16+
return (
17+
compareStrings(a.project.name, b.project.name) ||
18+
compareStrings(a.moduleId, b.moduleId) ||
19+
compareStrings(a.pool, b.pool)
20+
)
21+
}
22+
23+
// `localeCompare` depends on the locale of the machine, which makes `--seed` unreproducible
24+
function compareStrings(a: string, b: string): number {
25+
return a < b ? -1 : a > b ? 1 : 0
26+
}

‎test/unit/test/sequencers.test.ts‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ function buildCtx(config?: Partial<Vitest['config']>) {
1616
} as unknown as Vitest
1717
}
1818

19-
function buildWorkspace() {
19+
function buildWorkspace(name = 'test') {
2020
return {
21-
name: 'test',
21+
name,
2222
config: {
2323
root: import.meta.dirname,
2424
sequence: { groupOrder: 0 },
@@ -28,8 +28,8 @@ function buildWorkspace() {
2828

2929
const workspace = buildWorkspace()
3030

31-
function workspaced(files: string[]) {
32-
return files.map((file) => new TestSpecification(workspace, file, 'forks'))
31+
function workspaced(files: string[], project: TestProject = workspace) {
32+
return files.map((file) => new TestSpecification(project, file, 'forks'))
3333
}
3434

3535
describe('base sequencer', () => {
@@ -67,12 +67,48 @@ describe('base sequencer', () => {
6767
})
6868

6969
describe('random sequencer', () => {
70-
test('sorting is the same when seed is defined', async () => {
70+
function buildRandomSequencer(seed: number) {
7171
const ctx = buildCtx()
72-
ctx.config.sequence.seed = 101
73-
const sequencer = new RandomSequencer(ctx)
72+
ctx.config.sequence.seed = seed
73+
return new RandomSequencer(ctx)
74+
}
75+
76+
function names(specs: TestSpecification[]) {
77+
return specs.map((spec) => `${spec.project.name}/${spec.moduleId}`)
78+
}
79+
80+
test('sorting is the same when seed is defined', async () => {
81+
const sequencer = buildRandomSequencer(101)
82+
const sorted = await sequencer.sort(workspaced(['b', 'a', 'c']))
83+
expect(names(sorted)).toMatchInlineSnapshot(`
84+
[
85+
"test/b",
86+
"test/c",
87+
"test/a",
88+
]
89+
`)
90+
})
91+
92+
test('the order does not depend on the order of the input', async () => {
93+
const sequencer = buildRandomSequencer(101)
94+
const web = buildWorkspace('web')
95+
const node = buildWorkspace('node')
96+
const first = await sequencer.sort([
97+
...workspaced(['a', 'b', 'c'], web),
98+
...workspaced(['a', 'b', 'c'], node),
99+
])
100+
const second = await sequencer.sort([
101+
...workspaced(['c', 'a'], node),
102+
...workspaced(['b', 'c', 'a'], web),
103+
...workspaced(['b'], node),
104+
])
105+
expect(names(second)).toEqual(names(first))
106+
})
107+
108+
test('does not change the input', async () => {
109+
const sequencer = buildRandomSequencer(101)
74110
const files = workspaced(['b', 'a', 'c'])
75-
const sorted = await sequencer.sort(files)
76-
expect(sorted).toStrictEqual(workspaced(['a', 'c', 'b']))
111+
await sequencer.sort(files)
112+
expect(names(files)).toEqual(['test/b', 'test/a', 'test/c'])
77113
})
78114
})

0 commit comments

Comments
 (0)