-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathvitest.unit.config.ts
More file actions
151 lines (138 loc) · 4.31 KB
/
vitest.unit.config.ts
File metadata and controls
151 lines (138 loc) · 4.31 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
import { cpus } from "node:os";
import tsconfigPaths from "vite-tsconfig-paths";
import { configDefaults, defineConfig } from "vitest/config";
// Allow auth module (src/services/auth, tokens.ts) to load when any unit test imports it.
// NODE_ENV: only set when unset (?? "test"); does not override CI's NODE_ENV=production.
// API_AUTH_JWT_SECRET: when unset, this fallback prevents getSecret() from throwing TalawaRestError
// ("API_AUTH_JWT_SECRET must be set in production") so password.test.ts and other files can static-import auth.
process.env.NODE_ENV = process.env.NODE_ENV ?? "test";
if (!process.env.API_AUTH_JWT_SECRET) {
process.env.API_AUTH_JWT_SECRET = "unit-test-secret";
}
const isCI = !!process.env.CI;
const cpuCount = cpus().length;
const MAX_CI_THREADS = 12; // Reduced to leave headroom
const MAX_LOCAL_THREADS = 16;
// Allow override via env var for CI rollout testing
const envCiThreads = process.env.VITEST_CI_THREADS
? Number.parseInt(process.env.VITEST_CI_THREADS, 10)
: undefined;
const computedCiThreads = Math.min(
MAX_CI_THREADS,
Math.max(4, Math.floor(cpuCount * 0.85)), // Increased utilization
);
const ciThreads =
envCiThreads && !Number.isNaN(envCiThreads)
? envCiThreads
: computedCiThreads;
const localThreads = Math.min(MAX_LOCAL_THREADS, Math.max(4, cpuCount));
// Runtime observability for CI rollout
if (isCI) {
// const shardId = process.env.SHARD_INDEX || "unknown";
// console.log(
// `[Vitest Config] Shard ${shardId}: cpuCount=${cpuCount}, computedCiThreads=${computedCiThreads}, ciThreads=${ciThreads}`,
// );
if (ciThreads < cpuCount) {
// console.warn(
// `[Vitest Config] Thread count reduced from ${cpuCount} to ${ciThreads} to prevent over-subscription`,
// );
}
if (envCiThreads) {
// console.log(
// `[Vitest Config] Using VITEST_CI_THREADS override: ${ciThreads}`,
// );
}
}
// Skip global setup for pure unit tests that don't need server/db
export default defineConfig({
plugins: [tsconfigPaths()],
test: {
// Define unit test patterns
include: [
"test/unit/**/*.{test,spec}.ts",
"test/utilities/**/*.{test,spec}.ts",
"test/services/**/*.{test,spec}.ts",
"test/routes/**/*.{test,spec}.ts",
"test/observability/**/*.{test,spec}.ts",
"test/workers/**/*.{test,spec}.ts",
"test/config/**/*.{test,spec}.ts",
"test/fastifyPlugins/**/*.{test,spec}.ts",
"test/plugin/**/*.{test,spec}.ts",
"test/scripts/**/*.{test,spec}.ts",
"test/setup/**/*.{test,spec}.ts",
"test/unit_tests/**/*.{test,spec}.ts",
"test/helpers/**/*.{test,spec}.ts",
"test/*.{test,spec}.ts",
"src/**/*.{test,spec}.ts",
],
exclude: [
...configDefaults.exclude,
"dist/**",
"coverage/**",
"docs/**",
"**/*.d.ts",
"data/**",
"docker/**",
"drizzle_migrations/**",
"envFiles/**",
"scripts/**",
"**/scripts/**",
"test/drizzle/**",
"test/graphql/**",
"test/install/**",
],
coverage: {
provider: "v8",
reporter: ["text", "lcov", "html", "json"],
// Restrict unit coverage to unit-owned source paths
include: [
"src/config/**",
"src/fastifyPlugins/**",
"src/observability/**",
"src/plugin/**",
"src/plugins/**",
"src/routes/**",
"src/services/**",
"src/setup/**",
"src/types/**",
"src/utilities/**",
"src/workers/**",
"src/*.ts",
],
// Only include files that are actually touched by tests
all: false,
exclude: [
...(configDefaults.coverage?.exclude ?? []),
"dist/**",
"coverage/**",
"docs/**",
"**/*.d.ts",
"data/**",
"docker/**",
"drizzle_migrations/**",
"envFiles/**",
"**/scripts/**",
"vitest.*.config.ts",
],
},
// https://vitest.dev/config/#globalsetup
globalSetup: [],
// https://vitest.dev/config/#passwithnotests
passWithNoTests: true,
hookTimeout: 30000, // 30 seconds for hooks
testTimeout: 60000, // 60 seconds per test
pool: "threads", // for faster test execution and to avoid postgres max-limit error
isolate: true,
maxWorkers: isCI ? ciThreads : localThreads,
// Set maxConcurrency lower than maxWorkers to prevent single heavy test files
// from consuming all workers and blocking other test files
maxConcurrency: isCI
? Math.max(1, Math.floor(ciThreads / 2))
: Math.max(1, Math.floor(localThreads / 2)),
fileParallelism: true,
sequence: {
shuffle: false,
concurrent: false,
},
},
});