Skip to content

Commit 356db87

Browse files
authoredMar 14, 2024··
feat(config): add shuffle.files and shuffle.tests options (#5281)
1 parent 201bd06 commit 356db87

File tree

6 files changed

+104
-6
lines changed

6 files changed

+104
-6
lines changed
 

‎docs/config/index.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -1768,13 +1768,29 @@ Sharding is happening before sorting, and only if `--shard` option is provided.
17681768

17691769
#### sequence.shuffle
17701770

1771-
- **Type**: `boolean`
1771+
- **Type**: `boolean | { files?, tests? }`
17721772
- **Default**: `false`
17731773
- **CLI**: `--sequence.shuffle`, `--sequence.shuffle=false`
17741774

1775-
If you want tests to run randomly, you can enable it with this option, or CLI argument [`--sequence.shuffle`](/guide/cli).
1775+
If you want files and tests to run randomly, you can enable it with this option, or CLI argument [`--sequence.shuffle`](/guide/cli).
1776+
1777+
Vitest usually uses cache to sort tests, so long running tests start earlier - this makes tests run faster. If your files and tests will run in random order you will lose this performance improvement, but it may be useful to track tests that accidentally depend on another run previously.
1778+
1779+
#### sequence.shuffle.files <Badge type="info">1.4.0+</Badge> {#sequence-shuffle-files}
1780+
1781+
- **Type**: `boolean`
1782+
- **Default**: `false`
1783+
- **CLI**: `--sequence.shuffle.files`, `--sequence.shuffle.files=false`
1784+
1785+
Whether to randomize files, be aware that long running tests will not start earlier if you enable this option.
1786+
1787+
#### sequence.shuffle.tests <Badge type="info">1.4.0+</Badge> {#sequence-shuffle-tests}
1788+
1789+
- **Type**: `boolean`
1790+
- **Default**: `false`
1791+
- **CLI**: `--sequence.shuffle.tests`, `--sequence.shuffle.tests=false`
17761792

1777-
Vitest usually uses cache to sort tests, so long running tests start earlier - this makes tests run faster. If your tests will run in random order you will lose this performance improvement, but it may be useful to track tests that accidentally depend on another run previously.
1793+
Whether to randomize tests.
17781794

17791795
#### sequence.concurrent <Badge type="info">0.32.2+</Badge> {#sequence-concurrent}
17801796

‎packages/vitest/src/node/cli/cli-config.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,16 @@ export const cliOptionsConfig: VitestCLIOptions = {
436436
argument: '<options>',
437437
subcommands: {
438438
shuffle: {
439-
description: 'Run tests in a random order. Enabling this option will impact Vitest\'s cache and have a performance impact. May be useful to find tests that accidentally depend on another run previously (default: false)',
439+
description: 'Run files and tests in a random order. Enabling this option will impact Vitest\'s cache and have a performance impact. May be useful to find tests that accidentally depend on another run previously (default: false)',
440+
argument: '',
441+
subcommands: {
442+
files: {
443+
description: 'Run files in a random order. Long running tests will not start earlier if you enable this option. (default: false)',
444+
},
445+
tests: {
446+
description: 'Run tests in a random oder (default: false)',
447+
},
448+
},
440449
},
441450
concurrent: {
442451
description: 'Make tests run in parallel (default: false)',

‎packages/vitest/src/node/config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ export function resolveConfig(
441441
resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir, resolved.name)
442442

443443
resolved.sequence ??= {} as any
444+
if (resolved.sequence.shuffle && typeof resolved.sequence.shuffle === 'object') {
445+
const { files, tests } = resolved.sequence.shuffle
446+
resolved.sequence.sequencer ??= files ? RandomSequencer : BaseSequencer
447+
resolved.sequence.shuffle = tests
448+
}
444449
if (!resolved.sequence?.sequencer) {
445450
// CLI flag has higher priority
446451
resolved.sequence.sequencer = resolved.sequence.shuffle

‎packages/vitest/src/types/config.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,22 @@ interface SequenceOptions {
4848
*/
4949
sequencer?: TestSequencerConstructor
5050
/**
51-
* Should tests run in random order.
51+
* Should files and tests run in random order.
5252
* @default false
5353
*/
54-
shuffle?: boolean
54+
shuffle?: boolean | {
55+
/**
56+
* Should files run in random order. Long running tests will not start
57+
* earlier if you enable this option.
58+
* @default false
59+
*/
60+
files?: boolean
61+
/**
62+
* Should tests run in random order.
63+
* @default false
64+
*/
65+
tests?: boolean
66+
}
5567
/**
5668
* Should tests run in parallel.
5769
* @default false
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { InlineConfig } from 'vitest'
2+
import { expect, test } from 'vitest'
3+
import { runVitest } from '../../test-utils'
4+
5+
function run(sequence: InlineConfig['sequence']) {
6+
return runVitest({
7+
sequence,
8+
include: [],
9+
})
10+
}
11+
12+
class CustomSequencer {
13+
sort() {
14+
return []
15+
}
16+
17+
shard() {
18+
return []
19+
}
20+
}
21+
22+
test.each([
23+
false,
24+
{ files: false, tests: false },
25+
{ files: false, tests: true },
26+
],
27+
)('should use BaseSequencer if shuffle is %o', async (shuffle) => {
28+
const { vitest } = await run({ shuffle })
29+
expect(vitest?.config.sequence.sequencer.name).toBe('BaseSequencer')
30+
})
31+
32+
test.each([
33+
true,
34+
{ files: true, tests: false },
35+
{ files: true, tests: true },
36+
])('should use RandomSequencer if shuffle is %o', async (shuffle) => {
37+
const { vitest } = await run({ shuffle })
38+
expect(vitest?.config.sequence.sequencer.name).toBe('RandomSequencer')
39+
})
40+
41+
test.each([
42+
false,
43+
true,
44+
{ files: true, tests: false },
45+
{ files: true, tests: true },
46+
])('should always use CustomSequencer if passed', async (shuffle) => {
47+
const { vitest } = await run({ shuffle, sequencer: CustomSequencer })
48+
expect(vitest?.config.sequence.sequencer.name).toBe('CustomSequencer')
49+
})

‎test/core/test/cli-test.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ test('cache is parsed correctly', () => {
223223
})
224224
})
225225

226+
test('shuffle is parsed correctly', () => {
227+
expect(getCLIOptions('--sequence.shuffle')).toEqual({ sequence: { shuffle: true } })
228+
expect(getCLIOptions('--sequence.shuffle=false')).toEqual({ sequence: { shuffle: false } })
229+
expect(getCLIOptions('--sequence.shuffle.files --sequence.shuffle.tests')).toEqual({ sequence: { shuffle: { files: true, tests: true } } })
230+
expect(getCLIOptions('--sequence.shuffle.files=false --sequence.shuffle.tests=false')).toEqual({ sequence: { shuffle: { files: false, tests: false } } })
231+
})
232+
226233
test('typecheck correctly passes down arguments', () => {
227234
const { options, args } = parseArguments('--typecheck some.name.ts')
228235
expect(options).toEqual({ typecheck: { enabled: true } })

0 commit comments

Comments
 (0)
Please sign in to comment.