-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvitest.config.mts
More file actions
190 lines (169 loc) · 6.79 KB
/
vitest.config.mts
File metadata and controls
190 lines (169 loc) · 6.79 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import path from "path";
import os from "os";
/**
* Detect if we're running a targeted test (single file/directory) vs full suite.
* Targeted tests have limited parallelization potential.
*/
function isTargetedTest(): boolean {
// Check for test file arguments (not flags or flag values)
// Test files typically end with .test.* or .spec.* or contain __tests__
const args = process.argv.slice(2);
const testFilePattern = /\.(test|spec)\.[jt]sx?$|__tests__/;
for (let i = 0; i < args.length; i++) {
const arg = args[i];
// Skip flags and their values
if (arg.startsWith("-")) {
continue;
}
// Check if this looks like a test file path
if (testFilePattern.test(arg)) {
return true;
}
}
return false;
}
/**
* Intelligent worker count based on test scope and system load.
*
* The algorithm adapts to:
* 1. Test scope: Single file tests use fewer workers (limited parallelization)
* 2. System load: Tiered reduction only when system is actually struggling
*
* Full test suite strategy (DX-focused):
* - Load ratio < 1.2: Use 10 workers (optimal speed)
* - Load ratio 1.2-1.8: Use 8 workers (modest reduction)
* - Load ratio > 1.8: Use 5 workers (system struggling)
*
* Single file tests: Use 2-4 workers max (limited parallelization benefit)
*
* Benchmark results (14-core M3 Max, 2026-01-09):
* - 8 workers: 19.77s (OPTIMAL - virtually tied with 10)
* - 10 workers: 19.75s (0.02s faster, not meaningful)
* - 14 workers: 20.64s (+4.4% slower - resource contention)
* - 20 workers: 20.07s (+1.5% slower)
* - 28 workers: 24.09s (+21.9% slower)
*
* Conclusion: ~71% of cores (10/14 = 0.71) provides good parallelization without
* significant resource contention. This ratio scales across different CPU counts.
*/
function calculateOptimalWorkers(): {
workers: number;
cpuCount: number;
loadAvg: number;
isTargeted: boolean;
} {
const cpuCount = os.cpus().length;
const MIN_WORKERS = 2;
const isTargeted = isTargetedTest();
// Guard against containerized environments where cpuCount may be 0
if (cpuCount === 0) {
return { workers: MIN_WORKERS, cpuCount: 0, loadAvg: 0, isTargeted };
}
// Get 1-minute load average (lightweight kernel read)
// Note: Windows returns [0,0,0], so we'll use all cores there (same as CI)
const [loadAvg] = os.loadavg();
// CI: Always use all cores (dedicated environment)
if (process.env.CI) {
return { workers: cpuCount, cpuCount, loadAvg, isTargeted };
}
// Manual override via environment variable
if (process.env.VITEST_WORKERS) {
const manual = parseInt(process.env.VITEST_WORKERS, 10);
if (!isNaN(manual) && manual > 0) {
return { workers: Math.min(manual, cpuCount), cpuCount, loadAvg, isTargeted };
}
}
// Single file/directory tests: Use fewer workers (limited parallelization)
if (isTargeted) {
const targetedWorkers = Math.min(4, Math.max(MIN_WORKERS, Math.floor(cpuCount / 2)));
return { workers: targetedWorkers, cpuCount, loadAvg, isTargeted };
}
// Full test suite: Scale workers based on system load
// Optimal is ~71% of cores (10/14 = 0.71)
// This ratio balances parallelization with resource contention across different CPU counts
const OPTIMAL_RATIO = 0.71;
const OPTIMAL_WORKERS = Math.max(4, Math.ceil(cpuCount * OPTIMAL_RATIO));
// Calculate load ratio (0 = idle, 1 = fully loaded, >1 = overloaded)
const loadRatio = loadAvg / cpuCount;
// DX-focused scaling: Keep tests fast unless system is actually struggling
// Use tiered approach instead of linear scaling
let workers: number;
if (loadRatio < 1.2) {
// Low to moderate load: Full speed ahead
workers = OPTIMAL_WORKERS;
} else if (loadRatio < 1.8) {
// High load: Modest reduction (75% of optimal)
workers = Math.max(MIN_WORKERS, Math.ceil(OPTIMAL_WORKERS * 0.75));
} else {
// Very high load: System struggling, back off more (50% of optimal)
workers = Math.max(MIN_WORKERS, Math.ceil(OPTIMAL_WORKERS * 0.5));
}
return { workers, cpuCount, loadAvg, isTargeted };
}
const { workers: maxWorkers, cpuCount, loadAvg, isTargeted } = calculateOptimalWorkers();
// Log the decision for visibility (only in non-CI, when not silent)
if (!process.env.CI && !process.argv.includes("--silent")) {
let reason = "";
const scope = isTargeted ? "targeted test" : "full suite";
const manualWorkers = process.env.VITEST_WORKERS ? parseInt(process.env.VITEST_WORKERS, 10) : NaN;
const isValidManualOverride = !isNaN(manualWorkers) && manualWorkers > 0;
if (isValidManualOverride) {
reason = `manual override (VITEST_WORKERS=${process.env.VITEST_WORKERS})`;
} else if (cpuCount === 0) {
reason = "containerized environment fallback";
} else if (isTargeted) {
reason = `${scope} - limited parallelization`;
} else if (loadAvg === 0) {
reason = `${scope} - Windows/no load data`;
} else {
const loadRatio = loadAvg / cpuCount;
if (loadRatio < 1.2) {
reason = `${scope} - optimal speed (low/moderate load)`;
} else if (loadRatio < 1.8) {
reason = `${scope} - reduced load (ratio: ${loadRatio.toFixed(2)}, high load)`;
} else {
reason = `${scope} - reduced load (ratio: ${loadRatio.toFixed(2)}, system struggling)`;
}
}
console.log(
`\x1b[36m⚡ Vitest workers: ${maxWorkers}/${cpuCount} cores (load: ${loadAvg.toFixed(1)}) - ${reason}\x1b[0m`
);
}
export default defineConfig({
plugins: [react()],
test: {
environment: "jsdom",
setupFiles: [path.resolve(__dirname, "./vitest.setup.ts")],
include: [
"__tests__/unit/**/*.{test,spec}.{ts,tsx}",
"__tests__/integration/**/*.{test,spec}.{ts,tsx}",
],
// Forks vs Threads trade-off:
// - Threads: ~10-20% faster, but V8 threading bugs cause segfaults during cleanup
// - Forks: Slightly slower, but rock-solid stability (each worker is isolated process)
// We choose stability over speed. See: nodejs/node#58690, vitest-dev/vitest#1696
pool: "forks",
isolate: true,
maxWorkers,
coverage: {
provider: "v8",
reporter: ["text", "json", "html"],
exclude: [
"node_modules/",
"__tests__/",
"**/*.config.*",
"**/*.d.ts",
".next/",
"out/",
],
},
slowTestThreshold: 10000,
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./"),
},
},
});