-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvitest.config.ts
More file actions
118 lines (101 loc) · 3.3 KB
/
vitest.config.ts
File metadata and controls
118 lines (101 loc) · 3.3 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
import { existsSync, readdirSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { defineConfig, mergeConfig, type TestProjectConfiguration } from "vitest/config";
import { aliases } from "./vitest.aliases.ts";
import { normalize } from "node:path";
const pkgRoot = (root: string, pkg: string) =>
fileURLToPath(new URL(`./${root}/${pkg}`, import.meta.url));
type VitestConfigWithProjects = {
test?: {
projects?: TestProjectConfiguration[];
};
};
async function createProjects(root: string): Promise<TestProjectConfiguration[]> {
try {
const rootDir = fileURLToPath(new URL(`./${root}`, import.meta.url));
const dirs = readdirSync(rootDir).filter((dir) =>
existsSync(normalize(`${pkgRoot(root, dir)}/package.json`)),
);
const promises = dirs.map(async (dir) => {
const base = {
extends: true,
resolve: {
alias: aliases,
},
test: {
dir: `./${root}/${dir}/test`,
include: ["**/*.{test,spec}.?(c|m)[jt]s?(x)"],
name: dir,
},
} satisfies TestProjectConfiguration;
const nestedBase = {
extends: true,
resolve: {
alias: aliases,
},
test: {
dir: `./${root}/${dir}/test`,
},
} satisfies TestProjectConfiguration;
const customConfigPath = normalize(`${pkgRoot(root, dir)}/vitest.config.ts`);
if (existsSync(customConfigPath)) {
const safePath = customConfigPath.replace(/^[A-Z]:\\/, "/").replace(/\\/g, "/");
try {
const customConfig = await import(safePath).then((m) => m.default);
const nestedProjects = (customConfig as VitestConfigWithProjects).test?.projects;
if (Array.isArray(nestedProjects)) {
return nestedProjects.map((project) => {
if (typeof project !== "object" || project == null) {
return project;
}
return mergeConfig(nestedBase, project);
});
}
return mergeConfig(base, customConfig);
} catch (err) {
console.warn(
`[vitest] Failed to load custom config for ${root}/${dir} (path: ${customConfigPath}, safe path: ${safePath}):`,
err,
);
return base;
}
}
return base;
});
return (await Promise.all(promises)).flat();
} catch (err) {
console.warn(`[vitest] Failed to scan ${root} directory:`, err);
return [];
}
}
const packageProjects = await createProjects("packages");
const pipelinePackageProjects = await createProjects("packages/pipelines");
const toolingProjects = await createProjects("tooling");
const appProjects = await createProjects("apps");
const hiddenLogs: string[] = [];
export default defineConfig({
test: {
environment: "node",
mockReset: true,
setupFiles: [
"./packages/test-utils/src/msw/vitest-setup.ts",
"./packages/test-utils/src/matchers/vitest-setup.ts"
],
onConsoleLog(log, type) {
if (type === "stderr") {
return !hiddenLogs.some((hidden) => log.includes(hidden));
}
return false;
},
projects: [
...packageProjects,
...pipelinePackageProjects,
...toolingProjects,
...appProjects
]
},
oxc: {
target: "es2024"
},
resolve: { alias: aliases },
});