forked from dxos/dxos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.shared.ts
More file actions
167 lines (149 loc) · 4.63 KB
/
Copy pathvitest.shared.ts
File metadata and controls
167 lines (149 loc) · 4.63 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
//
// Copyright 2024 DXOS.org
//
import { join, relative } from 'node:path';
import pkgUp from 'pkg-up';
import { type Plugin, UserConfig as ViteConfig } from 'vite';
import { defineConfig, type UserConfig as VitestConfig } from 'vitest/config';
import WasmPlugin from 'vite-plugin-wasm';
import Inspect from 'vite-plugin-inspect';
import { FixGracefulFsPlugin, NodeExternalPlugin } from '@dxos/esbuild-plugins';
import { GLOBALS, MODULES } from '@dxos/node-std/_/config';
const targetProject = String(process.env.NX_TASK_TARGET_PROJECT);
const isDebug = !!process.env.VITEST_DEBUG;
const environment = (process.env.VITEST_ENV ?? 'node').toLowerCase();
const shouldCreateXmlReport = Boolean(process.env.VITEST_XML_REPORT);
const createNodeConfig = (cwd: string) =>
defineConfig({
esbuild: {
target: 'es2020',
},
test: {
...resolveReporterConfig({ browserMode: false, cwd }),
environment: 'node',
include: [
'**/src/**/*.test.{ts,tsx}',
'**/test/**/*.test.{ts,tsx}',
'!**/src/**/*.browser.test.{ts,tsx}',
'!**/test/**/*.browser.test.{ts,tsx}',
],
},
// Shows build trace
// VITE_INSPECT=1 pnpm vitest --ui
// http://localhost:51204/__inspect/#/
plugins: [process.env.VITE_INSPECT && Inspect()],
});
type BrowserOptions = {
browserName: string;
cwd: string;
nodeExternal?: boolean;
injectGlobals?: boolean;
};
const createBrowserConfig = ({ browserName, cwd, nodeExternal = false, injectGlobals = true }: BrowserOptions) =>
defineConfig({
plugins: [
nodeStdPlugin(),
WasmPlugin(),
// Inspect()
],
optimizeDeps: {
include: ['buffer/'],
esbuildOptions: {
plugins: [
FixGracefulFsPlugin(),
// TODO(wittjosiah): Compute nodeStd from package.json.
...(nodeExternal ? [NodeExternalPlugin({ injectGlobals, nodeStd: true })] : []),
],
},
},
esbuild: {
target: 'es2020',
},
test: {
...resolveReporterConfig({ browserMode: true, cwd }),
name: targetProject,
env: {
LOG_CONFIG: 'log-config.yaml',
},
include: [
'**/src/**/*.test.{ts,tsx}',
'**/test/**/*.test.{ts,tsx}',
'!**/src/**/*.node.test.{ts,tsx}',
'!**/test/**/*.node.test.{ts,tsx}',
],
testTimeout: isDebug ? 9999999 : 5000,
inspect: isDebug,
isolate: false,
poolOptions: {
threads: {
singleThread: true,
},
},
browser: {
enabled: true,
screenshotFailures: false,
headless: !isDebug,
provider: 'playwright',
name: browserName,
isolate: false,
},
},
});
const resolveReporterConfig = ({ browserMode, cwd }: { browserMode: boolean; cwd: string }): VitestConfig['test'] => {
const packageJson = pkgUp.sync({ cwd });
const packageDir = packageJson!.split('/').slice(0, -1).join('/');
const packageDirRelative = relative(__dirname, packageDir);
const coverageDir = join(__dirname, 'coverage', packageDirRelative);
if (shouldCreateXmlReport) {
return {
passWithNoTests: true,
reporters: ['junit', 'verbose'],
// TODO(wittjosiah): Browser mode will overwrite this, should be separate directories
// however nx outputs config also needs to be aware of this.
outputFile: join(__dirname, 'test-results', packageDirRelative, 'results.xml'),
coverage: {
reportsDirectory: coverageDir,
},
};
}
return {
passWithNoTests: true,
reporters: ['verbose'],
coverage: {
reportsDirectory: coverageDir,
},
};
};
export type ConfigOptions = Omit<BrowserOptions, 'browserName'>;
export const baseConfig = (options: ConfigOptions = {}): ViteConfig => {
switch (environment) {
case 'chromium':
return createBrowserConfig({ browserName: environment, ...options });
case 'node':
default:
if (environment.length > 0 && environment !== 'node') {
console.log("Unrecognized VITEST_ENV value, falling back to 'node': " + environment);
}
return createNodeConfig(options.cwd);
}
};
/**
* Replaces node built-in modules with their browser equivalents.
*/
// TODO(dmaretskyi): Extract.
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);
}
},
},
};
}