-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
vitestSetup.ts
380 lines (342 loc) · 9.8 KB
/
vitestSetup.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import type * as http from 'node:http'
import fs from 'node:fs'
import path from 'node:path'
import { chromium } from 'playwright-chromium'
import type {
ConfigEnv,
InlineConfig,
Logger,
PluginOption,
ResolvedConfig,
UserConfig,
ViteDevServer,
} from 'vite'
import {
build,
createBuilder,
createServer,
loadConfigFromFile,
mergeConfig,
preview,
} from 'vite'
import type { Browser, Page } from 'playwright-chromium'
import type { RollupError, RollupWatcher, RollupWatcherEvent } from 'rollup'
import type { RunnerTestFile } from 'vitest'
import { beforeAll, inject } from 'vitest'
// #region env
export const workspaceRoot = path.resolve(__dirname, '../')
export const isBuild = !!process.env.VITE_TEST_BUILD
export const isServe = !isBuild
export const isWindows = process.platform === 'win32'
export const viteBinPath = path.posix.join(
workspaceRoot,
'packages/vite/bin/vite.js',
)
// #endregion
// #region context
let server: ViteDevServer | http.Server
/**
* Vite Dev Server when testing serve
*/
export let viteServer: ViteDevServer
/**
* Root of the Vite fixture
*/
export let rootDir: string
/**
* Path to the current test file
*/
export let testPath: string
/**
* Path to the test folder
*/
export let testDir: string
/**
* Test folder name
*/
export let testName: string
export const serverLogs: string[] = []
export const browserLogs: string[] = []
export const browserErrors: Error[] = []
export let resolvedConfig: ResolvedConfig = undefined!
export let page: Page = undefined!
export let browser: Browser = undefined!
export let viteTestUrl: string = ''
export let watcher: RollupWatcher | undefined = undefined
export function setViteUrl(url: string): void {
viteTestUrl = url
}
// #endregion
beforeAll(async (s) => {
const suite = s as RunnerTestFile
testPath = suite.filepath!
testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1]
testDir = path.dirname(testPath)
if (testName) {
testDir = path.resolve(workspaceRoot, 'playground-temp', testName)
}
// skip browser setup for non-playground tests
// TODO: ssr playground?
if (
!suite.filepath.includes('playground') ||
suite.filepath.includes('hmr-ssr')
) {
return
}
const wsEndpoint = inject('wsEndpoint')
if (!wsEndpoint) {
throw new Error('wsEndpoint not found')
}
browser = await chromium.connect(wsEndpoint)
page = await browser.newPage()
const globalConsole = global.console
const warn = globalConsole.warn
globalConsole.warn = (msg, ...args) => {
// suppress @vue/reactivity-transform warning
if (msg.includes('@vue/reactivity-transform')) return
if (msg.includes('Generated an empty chunk')) return
warn.call(globalConsole, msg, ...args)
}
try {
page.on('console', (msg) => {
// ignore favicon request in headed browser
if (
process.env.VITE_DEBUG_SERVE &&
msg.text().includes('Failed to load resource:') &&
msg.location().url.includes('favicon.ico')
) {
return
}
browserLogs.push(msg.text())
})
page.on('pageerror', (error) => {
browserErrors.push(error)
})
// if this is a test placed under playground/xxx/__tests__
// start a vite server in that directory.
if (testName) {
// when `root` dir is present, use it as vite's root
const testCustomRoot = path.resolve(testDir, 'root')
rootDir = fs.existsSync(testCustomRoot) ? testCustomRoot : testDir
// separate rootDir for variant
const variantName = path.basename(path.dirname(testPath))
if (variantName !== '__tests__') {
const variantTestDir = testDir + '__' + variantName
if (fs.existsSync(variantTestDir)) {
rootDir = testDir = variantTestDir
}
}
const testCustomServe = [
path.resolve(path.dirname(testPath), 'serve.ts'),
path.resolve(path.dirname(testPath), 'serve.js'),
].find((i) => fs.existsSync(i))
if (testCustomServe) {
// test has custom server configuration.
const mod = await import(testCustomServe)
const serve = mod.serve || mod.default?.serve
const preServe = mod.preServe || mod.default?.preServe
if (preServe) {
await preServe()
}
if (serve) {
server = await serve()
viteServer = mod.viteServer
}
} else {
await startDefaultServe()
}
}
} catch (e) {
// Closing the page since an error in the setup, for example a runtime error
// when building the playground should skip further tests.
// If the page remains open, a command like `await page.click(...)` produces
// a timeout with an exception that hides the real error in the console.
await page.close()
await server?.close()
throw e
}
return async () => {
serverLogs.length = 0
await page?.close()
await server?.close()
await watcher?.close()
if (browser) {
await browser.close()
}
}
})
async function loadConfig(configEnv: ConfigEnv) {
let config: UserConfig | null = null
// config file named by convention as the *.spec.ts folder
const variantName = path.basename(path.dirname(testPath))
if (variantName !== '__tests__') {
const configVariantPath = path.resolve(
rootDir,
`vite.config-${variantName}.js`,
)
if (fs.existsSync(configVariantPath)) {
const res = await loadConfigFromFile(configEnv, configVariantPath)
if (res) {
config = res.config
}
}
}
// config file from test root dir
if (!config) {
const res = await loadConfigFromFile(configEnv, undefined, rootDir)
if (res) {
config = res.config
}
}
const options: InlineConfig = {
root: rootDir,
logLevel: 'silent',
configFile: false,
server: {
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100,
},
fs: {
strict: !isBuild,
},
},
build: {
// esbuild do not minify ES lib output since that would remove pure annotations and break tree-shaking
// skip transpilation during tests to make it faster
target: 'esnext',
// tests are flaky when `emptyOutDir` is `true`
emptyOutDir: false,
},
customLogger: createInMemoryLogger(serverLogs),
}
return mergeConfig(options, config || {})
}
export async function startDefaultServe(): Promise<void> {
setupConsoleWarnCollector(serverLogs)
if (!isBuild) {
process.env.VITE_INLINE = 'inline-serve'
const config = await loadConfig({ command: 'serve', mode: 'development' })
viteServer = server = await (await createServer(config)).listen()
viteTestUrl = server.resolvedUrls.local[0]
if (server.config.base === '/') {
viteTestUrl = viteTestUrl.replace(/\/$/, '')
}
await page.goto(viteTestUrl)
} else {
process.env.VITE_INLINE = 'inline-build'
// determine build watch
const resolvedPlugin: () => PluginOption = () => ({
name: 'vite-plugin-watcher',
configResolved(config) {
resolvedConfig = config
},
})
const buildConfig = mergeConfig(
await loadConfig({ command: 'build', mode: 'production' }),
{
plugins: [resolvedPlugin()],
},
)
if (buildConfig.builder) {
const builder = await createBuilder(buildConfig)
await builder.buildApp()
} else {
const rollupOutput = await build(buildConfig)
const isWatch = !!resolvedConfig!.build.watch
// in build watch,call startStaticServer after the build is complete
if (isWatch) {
watcher = rollupOutput as RollupWatcher
await notifyRebuildComplete(watcher)
}
if (buildConfig.__test__) {
buildConfig.__test__()
}
}
const previewConfig = await loadConfig({
command: 'serve',
mode: 'development',
isPreview: true,
})
const _nodeEnv = process.env.NODE_ENV
const previewServer = await preview(previewConfig)
// prevent preview change NODE_ENV
process.env.NODE_ENV = _nodeEnv
viteTestUrl = previewServer.resolvedUrls.local[0]
await page.goto(viteTestUrl)
}
}
/**
* Send the rebuild complete message in build watch
*/
export async function notifyRebuildComplete(
watcher: RollupWatcher,
): Promise<RollupWatcher> {
let resolveFn: undefined | (() => void)
const callback = (event: RollupWatcherEvent): void => {
if (event.code === 'END') {
resolveFn?.()
}
}
watcher.on('event', callback)
await new Promise<void>((resolve) => {
resolveFn = resolve
})
return watcher.off('event', callback)
}
export function createInMemoryLogger(logs: string[]): Logger {
const loggedErrors = new WeakSet<Error | RollupError>()
const warnedMessages = new Set<string>()
const logger: Logger = {
hasWarned: false,
hasErrorLogged: (err) => loggedErrors.has(err),
clearScreen: () => {},
info(msg) {
logs.push(msg)
},
warn(msg) {
logs.push(msg)
logger.hasWarned = true
},
warnOnce(msg) {
if (warnedMessages.has(msg)) return
logs.push(msg)
logger.hasWarned = true
warnedMessages.add(msg)
},
error(msg, opts) {
logs.push(msg)
if (opts?.error) {
loggedErrors.add(opts.error)
}
},
}
return logger
}
function setupConsoleWarnCollector(logs: string[]) {
const warn = console.warn
console.warn = (...args) => {
logs.push(args.join(' '))
return warn.call(console, ...args)
}
}
export function slash(p: string): string {
return p.replace(/\\/g, '/')
}
declare module 'vite' {
export interface UserConfig {
/**
* special test only hook
*
* runs after build and before preview
*/
__test__?: () => void
}
}
declare module 'vitest' {
export interface ProvidedContext {
wsEndpoint: string
}
}