-
Notifications
You must be signed in to change notification settings - Fork 0
/
vitest.shared.ts
122 lines (109 loc) · 3.14 KB
/
vitest.shared.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
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
//
// Copyright 2024 DXOS.org
//
import { join } from 'path';
import { defineConfig, type UserConfig } from 'vitest/config';
import { type Plugin } from 'vite';
// import Inspect from 'vite-plugin-inspect';
import inject from '@rollup/plugin-inject';
import { GLOBALS, MODULES } from '@dxos/node-std/_/config';
import { FixGracefulFsPlugin } from '@dxos/esbuild-plugins';
const targetProject = String(process.env.NX_TASK_TARGET_PROJECT);
const isDebug = !!process.env.VITEST_DEBUG;
const environment = (process.env.VITEST_ENV ?? '').toLowerCase();
const shouldCreateXmlReport = Boolean(process.env.VITEST_XML_REPORT);
const createNodeConfig = () =>
defineConfig({
esbuild: {
target: 'es2020',
},
test: {
...resolveReporterConfig({ browserMode: false }),
environment: 'node',
include: ['**/src/**/*.test.ts', '!**/src/**/*.browser.test.ts'],
},
});
const createBrowserConfig = (browserName: 'chrome') =>
defineConfig({
plugins: [
nodeStdPlugin(),
// Inspect()
],
optimizeDeps: {
include: ['buffer/'],
esbuildOptions: {
plugins: [FixGracefulFsPlugin()],
},
},
esbuild: {
target: 'es2020',
// TODO(dmaretskyi): Move into nodeStd plugin.
banner: 'import "@dxos/node-std/globals";',
},
test: {
...resolveReporterConfig({ browserMode: true }),
name: targetProject,
include: ['**/src/**/*.test.ts', '!**/src/**/*.node.test.ts'],
testTimeout: isDebug ? 9999999 : 5000,
inspect: isDebug,
isolate: false,
poolOptions: {
threads: {
singleThread: true,
},
},
browser: {
enabled: true,
headless: !isDebug,
name: browserName,
isolate: false,
},
},
});
const resolveReporterConfig = (args: { browserMode: boolean }): UserConfig['test'] => {
if (shouldCreateXmlReport) {
const vitestReportDir = `vitest${args.browserMode ? '-browser' : ''}-reports`;
return {
passWithNoTests: true,
reporters: ['junit', 'verbose'],
outputFile: join(__dirname, `test-results/${vitestReportDir}/${targetProject}/report.xml`),
};
}
return {
passWithNoTests: true,
reporters: ['verbose'],
};
};
const resolveConfig = () => {
switch (environment) {
case 'chrome':
return createBrowserConfig(environment);
case 'node':
default:
if (environment.length > 0 && environment !== 'node') {
console.log("Unrecognized VITEST_ENV value, falling back to 'node': " + environment);
}
return createNodeConfig();
}
};
export default resolveConfig();
// TODO(dmaretskyi): Extract.
/**
* Replaces node built-in modules with their browser equivalents.
*/
function nodeStdPlugin(): Plugin {
return {
name: 'node-std',
resolveId: {
order: 'pre',
async handler(source, importer, options) {
if (source.startsWith('node:')) {
return this.resolve('@dxos/node-std/' + source.slice('node:'.length), importer, options);
}
if (MODULES.includes(source)) {
return this.resolve('@dxos/node-std/' + source, importer, options);
}
},
},
};
}