-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathvitest.config.ts
78 lines (69 loc) · 2.37 KB
/
vitest.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { relative } from 'node:path'
import { defineConfig } from 'vitest/config'
import { W } from 'vitest/dist/reporters-5f784f42'
import { BaseSequencer } from 'vitest/node'
/**
* Tests that might influence others and should run on an isolated executor (shard)
* Needs to be relative paths to the repository root.
*/
const RUN_ISOLATED = new Set([
'tests/integration/fetch-handler.test.ts',
'tests/integration/revalidate-path.test.ts',
'tests/integration/cache-handler.test.ts',
])
class Sequencer extends BaseSequencer {
async shard(projects: W[]): Promise<W[]> {
const {
config: { shard: { index = 1, count = 1 } = {} },
} = this.ctx
if (count === 1) {
console.log('[Sequencer]: No test sharding configured please specify with --shard 1/3')
return projects
}
console.log(`[Sequencer]: Sharding configured to run on ${index}/${count}`)
if (RUN_ISOLATED.size + 1 > count) {
throw new Error(
`[Sequencer]: The number of special tests + 1 for the remaining tests (${RUN_ISOLATED.size}) is larger than the node count (${count})
Increasing the node count of the sharding to --shard 1/${RUN_ISOLATED.size}`,
)
}
const specialTests = projects.filter((project) =>
RUN_ISOLATED.has(relative(process.cwd(), project[1])),
)
const regularTests = projects.filter(
(project) => !RUN_ISOLATED.has(relative(process.cwd(), project[1])),
)
// Allocate the first nodes for special tests
if (index <= RUN_ISOLATED.size) {
return [specialTests[index - 1]]
}
// Distribute remaining tests on the remaining nodes
if (index > RUN_ISOLATED.size && index <= count) {
const remainingNodes = count - RUN_ISOLATED.size
const bucketSize = Math.ceil(regularTests.length / remainingNodes)
const startIndex = (index - RUN_ISOLATED.size - 1) * bucketSize
const endIndex = startIndex + bucketSize
return regularTests.slice(startIndex, endIndex)
}
return projects
}
}
export default defineConfig({
root: '.',
test: {
include: ['{tests/integration,src}/**/*.test.ts'],
globals: true,
restoreMocks: true,
clearMocks: true,
mockReset: true,
unstubEnvs: true,
unstubGlobals: true,
environment: 'node',
testTimeout: 100000,
setupFiles: ['tests/test-setup.ts'],
logHeapUsage: true,
sequence: {
sequencer: Sequencer,
},
},
})