2022-09-23 13:13:51 +00:00
|
|
|
import type * as http from 'node:http'
|
2024-07-29 11:20:03 +00:00
|
|
|
import fs from 'node:fs'
|
|
|
|
import path from 'node:path'
|
2022-05-11 06:33:20 +00:00
|
|
|
import { chromium } from 'playwright-chromium'
|
2021-12-19 11:35:25 +00:00
|
|
|
import type {
|
2023-07-07 11:09:18 +00:00
|
|
|
ConfigEnv,
|
2022-03-25 18:33:45 +00:00
|
|
|
InlineConfig,
|
2022-05-11 09:03:19 +00:00
|
|
|
Logger,
|
2021-06-21 15:50:26 +00:00
|
|
|
PluginOption,
|
2021-12-01 16:35:39 +00:00
|
|
|
ResolvedConfig,
|
2022-08-21 15:23:21 +00:00
|
|
|
UserConfig,
|
2022-05-11 09:03:19 +00:00
|
|
|
ViteDevServer,
|
2021-06-21 15:50:26 +00:00
|
|
|
} from 'vite'
|
2022-09-23 13:13:51 +00:00
|
|
|
import {
|
|
|
|
build,
|
2024-09-04 09:27:09 +00:00
|
|
|
createBuilder,
|
2022-09-23 13:13:51 +00:00
|
|
|
createServer,
|
|
|
|
loadConfigFromFile,
|
|
|
|
mergeConfig,
|
|
|
|
preview,
|
|
|
|
} from 'vite'
|
2022-05-11 10:09:52 +00:00
|
|
|
import type { Browser, Page } from 'playwright-chromium'
|
2021-12-19 11:35:25 +00:00
|
|
|
import type { RollupError, RollupWatcher, RollupWatcherEvent } from 'rollup'
|
2024-10-24 10:14:48 +00:00
|
|
|
import type { RunnerTestFile } from 'vitest'
|
2024-01-16 19:49:24 +00:00
|
|
|
import { beforeAll, inject } from 'vitest'
|
2020-12-20 03:33:13 +00:00
|
|
|
|
2022-05-12 05:12:43 +00:00
|
|
|
// #region env
|
|
|
|
|
2024-07-29 11:20:03 +00:00
|
|
|
export const workspaceRoot = path.resolve(__dirname, '../')
|
2022-05-12 05:12:43 +00:00
|
|
|
|
|
|
|
export const isBuild = !!process.env.VITE_TEST_BUILD
|
|
|
|
export const isServe = !isBuild
|
|
|
|
export const isWindows = process.platform === 'win32'
|
2022-06-13 17:34:46 +00:00
|
|
|
export const viteBinPath = path.posix.join(
|
|
|
|
workspaceRoot,
|
|
|
|
'packages/vite/bin/vite.js',
|
|
|
|
)
|
2022-05-12 05:12:43 +00:00
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region context
|
2020-12-20 03:33:13 +00:00
|
|
|
|
|
|
|
let server: ViteDevServer | http.Server
|
2022-05-12 05:12:43 +00:00
|
|
|
|
2022-07-04 08:43:11 +00:00
|
|
|
/**
|
|
|
|
* Vite Dev Server when testing serve
|
|
|
|
*/
|
|
|
|
export let viteServer: ViteDevServer
|
2022-05-12 05:12:43 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2021-09-18 18:03:15 +00:00
|
|
|
|
2022-05-11 09:03:19 +00:00
|
|
|
export const serverLogs: string[] = []
|
|
|
|
export const browserLogs: string[] = []
|
|
|
|
export const browserErrors: Error[] = []
|
|
|
|
|
2022-05-18 05:07:41 +00:00
|
|
|
export let resolvedConfig: ResolvedConfig = undefined!
|
|
|
|
|
2022-05-11 09:03:19 +00:00
|
|
|
export let page: Page = undefined!
|
|
|
|
export let browser: Browser = undefined!
|
|
|
|
export let viteTestUrl: string = ''
|
|
|
|
export let watcher: RollupWatcher | undefined = undefined
|
|
|
|
|
2022-06-13 17:34:46 +00:00
|
|
|
export function setViteUrl(url: string): void {
|
2022-05-11 09:03:19 +00:00
|
|
|
viteTestUrl = url
|
2020-12-23 23:15:09 +00:00
|
|
|
}
|
2020-12-23 04:07:57 +00:00
|
|
|
|
2022-05-12 05:12:43 +00:00
|
|
|
// #endregion
|
2022-04-20 09:12:43 +00:00
|
|
|
|
2022-05-11 06:33:20 +00:00
|
|
|
beforeAll(async (s) => {
|
2024-10-24 10:14:48 +00:00
|
|
|
const suite = s as RunnerTestFile
|
2024-09-12 09:34:07 +00:00
|
|
|
|
|
|
|
testPath = suite.filepath!
|
|
|
|
testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1]
|
|
|
|
testDir = path.dirname(testPath)
|
|
|
|
if (testName) {
|
|
|
|
testDir = path.resolve(workspaceRoot, 'playground-temp', testName)
|
|
|
|
}
|
|
|
|
|
2022-05-11 06:33:20 +00:00
|
|
|
// skip browser setup for non-playground tests
|
2024-02-01 11:59:21 +00:00
|
|
|
// TODO: ssr playground?
|
|
|
|
if (
|
|
|
|
!suite.filepath.includes('playground') ||
|
|
|
|
suite.filepath.includes('hmr-ssr')
|
|
|
|
) {
|
2021-01-25 20:02:57 +00:00
|
|
|
return
|
|
|
|
}
|
2022-05-11 06:33:20 +00:00
|
|
|
|
2024-01-16 19:49:24 +00:00
|
|
|
const wsEndpoint = inject('wsEndpoint')
|
2022-05-12 05:12:43 +00:00
|
|
|
if (!wsEndpoint) {
|
|
|
|
throw new Error('wsEndpoint not found')
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:03:19 +00:00
|
|
|
browser = await chromium.connect(wsEndpoint)
|
|
|
|
page = await browser.newPage()
|
2022-05-11 06:33:20 +00:00
|
|
|
|
2022-05-12 03:55:54 +00:00
|
|
|
const globalConsole = global.console
|
2022-05-11 06:33:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-12-20 03:33:13 +00:00
|
|
|
try {
|
2022-05-11 09:03:19 +00:00
|
|
|
page.on('console', (msg) => {
|
2022-09-26 07:15:45 +00:00
|
|
|
// 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
|
|
|
|
}
|
2022-05-11 09:03:19 +00:00
|
|
|
browserLogs.push(msg.text())
|
|
|
|
})
|
|
|
|
page.on('pageerror', (error) => {
|
|
|
|
browserErrors.push(error)
|
|
|
|
})
|
2020-12-23 23:15:09 +00:00
|
|
|
|
2020-12-20 03:33:13 +00:00
|
|
|
// if this is a test placed under playground/xxx/__tests__
|
|
|
|
// start a vite server in that directory.
|
|
|
|
if (testName) {
|
2021-06-25 18:06:08 +00:00
|
|
|
// when `root` dir is present, use it as vite's root
|
2024-07-29 11:20:03 +00:00
|
|
|
const testCustomRoot = path.resolve(testDir, 'root')
|
2022-05-12 05:12:43 +00:00
|
|
|
rootDir = fs.existsSync(testCustomRoot) ? testCustomRoot : testDir
|
2021-06-25 18:06:08 +00:00
|
|
|
|
2024-07-30 05:35:18 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 06:33:20 +00:00
|
|
|
const testCustomServe = [
|
2024-07-29 11:20:03 +00:00
|
|
|
path.resolve(path.dirname(testPath), 'serve.ts'),
|
|
|
|
path.resolve(path.dirname(testPath), 'serve.js'),
|
2022-05-11 06:33:20 +00:00
|
|
|
].find((i) => fs.existsSync(i))
|
2022-05-12 05:12:43 +00:00
|
|
|
|
2022-05-11 06:33:20 +00:00
|
|
|
if (testCustomServe) {
|
2021-01-25 22:07:57 +00:00
|
|
|
// test has custom server configuration.
|
2022-05-11 06:33:20 +00:00
|
|
|
const mod = await import(testCustomServe)
|
|
|
|
const serve = mod.serve || mod.default?.serve
|
|
|
|
const preServe = mod.preServe || mod.default?.preServe
|
2021-09-24 20:21:04 +00:00
|
|
|
if (preServe) {
|
2022-05-12 05:12:43 +00:00
|
|
|
await preServe()
|
2021-09-24 20:21:04 +00:00
|
|
|
}
|
|
|
|
if (serve) {
|
2022-05-12 05:12:43 +00:00
|
|
|
server = await serve()
|
2022-07-04 08:43:11 +00:00
|
|
|
viteServer = mod.viteServer
|
2021-09-24 20:21:04 +00:00
|
|
|
}
|
2020-12-20 03:33:13 +00:00
|
|
|
} else {
|
2022-05-12 05:12:43 +00:00
|
|
|
await startDefaultServe()
|
2020-12-20 03:33:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-12 05:12:43 +00:00
|
|
|
} catch (e) {
|
2021-05-05 03:34:14 +00:00
|
|
|
// Closing the page since an error in the setup, for example a runtime error
|
2021-04-28 16:50:44 +00:00
|
|
|
// 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()
|
2022-05-12 05:12:43 +00:00
|
|
|
await server?.close()
|
|
|
|
throw e
|
2020-12-20 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 06:33:20 +00:00
|
|
|
return async () => {
|
2022-05-11 09:03:19 +00:00
|
|
|
serverLogs.length = 0
|
2022-05-11 06:33:20 +00:00
|
|
|
await page?.close()
|
|
|
|
await server?.close()
|
2022-09-06 16:57:10 +00:00
|
|
|
await watcher?.close()
|
2022-05-11 06:33:20 +00:00
|
|
|
if (browser) {
|
|
|
|
await browser.close()
|
|
|
|
}
|
2020-12-20 03:33:13 +00:00
|
|
|
}
|
2022-05-11 14:23:27 +00:00
|
|
|
})
|
2020-12-20 03:33:13 +00:00
|
|
|
|
2023-11-06 08:32:24 +00:00
|
|
|
async function loadConfig(configEnv: ConfigEnv) {
|
2022-08-21 15:23:21 +00:00
|
|
|
let config: UserConfig | null = null
|
2023-07-07 11:09:18 +00:00
|
|
|
|
|
|
|
// config file named by convention as the *.spec.ts folder
|
2024-07-29 11:20:03 +00:00
|
|
|
const variantName = path.basename(path.dirname(testPath))
|
2023-07-07 11:09:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-08-21 15:23:21 +00:00
|
|
|
}
|
|
|
|
// config file from test root dir
|
|
|
|
if (!config) {
|
2023-07-07 11:09:18 +00:00
|
|
|
const res = await loadConfigFromFile(configEnv, undefined, rootDir)
|
2022-08-21 15:23:21 +00:00
|
|
|
if (res) {
|
|
|
|
config = res.config
|
|
|
|
}
|
2022-05-12 05:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const options: InlineConfig = {
|
|
|
|
root: rootDir,
|
|
|
|
logLevel: 'silent',
|
2022-08-21 15:23:21 +00:00
|
|
|
configFile: false,
|
2022-05-12 05:12:43 +00:00
|
|
|
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
|
2022-06-08 08:39:57 +00:00
|
|
|
target: 'esnext',
|
|
|
|
// tests are flaky when `emptyOutDir` is `true`
|
|
|
|
emptyOutDir: false,
|
2022-05-12 05:12:43 +00:00
|
|
|
},
|
|
|
|
customLogger: createInMemoryLogger(serverLogs),
|
|
|
|
}
|
2023-11-06 08:32:24 +00:00
|
|
|
return mergeConfig(options, config || {})
|
|
|
|
}
|
2022-05-12 05:12:43 +00:00
|
|
|
|
2023-11-06 08:32:24 +00:00
|
|
|
export async function startDefaultServe(): Promise<void> {
|
2022-05-12 05:12:43 +00:00
|
|
|
setupConsoleWarnCollector(serverLogs)
|
|
|
|
|
|
|
|
if (!isBuild) {
|
|
|
|
process.env.VITE_INLINE = 'inline-serve'
|
2023-11-06 08:32:24 +00:00
|
|
|
const config = await loadConfig({ command: 'serve', mode: 'development' })
|
|
|
|
viteServer = server = await (await createServer(config)).listen()
|
2023-10-12 11:08:47 +00:00
|
|
|
viteTestUrl = server.resolvedUrls.local[0]
|
|
|
|
if (server.config.base === '/') {
|
|
|
|
viteTestUrl = viteTestUrl.replace(/\/$/, '')
|
|
|
|
}
|
2022-05-12 05:12:43 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
})
|
2023-11-06 08:32:24 +00:00
|
|
|
const buildConfig = mergeConfig(
|
|
|
|
await loadConfig({ command: 'build', mode: 'production' }),
|
|
|
|
{
|
|
|
|
plugins: [resolvedPlugin()],
|
|
|
|
},
|
|
|
|
)
|
2024-09-04 09:27:09 +00:00
|
|
|
if (buildConfig.builder) {
|
2024-09-26 07:54:49 +00:00
|
|
|
const builder = await createBuilder(buildConfig)
|
2024-09-04 09:27:09 +00:00
|
|
|
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__()
|
|
|
|
}
|
2023-10-03 10:13:03 +00:00
|
|
|
}
|
2023-11-06 08:32:24 +00:00
|
|
|
|
|
|
|
const previewConfig = await loadConfig({
|
|
|
|
command: 'serve',
|
|
|
|
mode: 'development',
|
|
|
|
isPreview: true,
|
|
|
|
})
|
2022-09-23 13:13:51 +00:00
|
|
|
const _nodeEnv = process.env.NODE_ENV
|
2023-11-06 08:32:24 +00:00
|
|
|
const previewServer = await preview(previewConfig)
|
2022-09-23 13:13:51 +00:00
|
|
|
// prevent preview change NODE_ENV
|
|
|
|
process.env.NODE_ENV = _nodeEnv
|
|
|
|
viteTestUrl = previewServer.resolvedUrls.local[0]
|
|
|
|
await page.goto(viteTestUrl)
|
|
|
|
}
|
2020-12-20 03:33:13 +00:00
|
|
|
}
|
2021-06-21 15:50:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the rebuild complete message in build watch
|
|
|
|
*/
|
|
|
|
export async function notifyRebuildComplete(
|
|
|
|
watcher: RollupWatcher,
|
|
|
|
): Promise<RollupWatcher> {
|
2022-05-03 07:55:03 +00:00
|
|
|
let resolveFn: undefined | (() => void)
|
|
|
|
const callback = (event: RollupWatcherEvent): void => {
|
|
|
|
if (event.code === 'END') {
|
|
|
|
resolveFn?.()
|
2021-06-21 15:50:26 +00:00
|
|
|
}
|
2022-05-03 07:55:03 +00:00
|
|
|
}
|
|
|
|
watcher.on('event', callback)
|
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
resolveFn = resolve
|
2021-06-21 15:50:26 +00:00
|
|
|
})
|
2022-11-07 08:35:36 +00:00
|
|
|
return watcher.off('event', callback)
|
2021-06-21 15:50:26 +00:00
|
|
|
}
|
2021-12-01 16:35:39 +00:00
|
|
|
|
2023-04-25 15:53:04 +00:00
|
|
|
export function createInMemoryLogger(logs: string[]): Logger {
|
2021-12-01 16:35:39 +00:00
|
|
|
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
|
|
|
|
}
|
2022-03-21 23:09:23 +00:00
|
|
|
|
|
|
|
function setupConsoleWarnCollector(logs: string[]) {
|
|
|
|
const warn = console.warn
|
|
|
|
console.warn = (...args) => {
|
2024-09-12 09:28:36 +00:00
|
|
|
logs.push(args.join(' '))
|
2022-03-21 23:09:23 +00:00
|
|
|
return warn.call(console, ...args)
|
|
|
|
}
|
|
|
|
}
|
2022-05-12 05:12:43 +00:00
|
|
|
|
|
|
|
export function slash(p: string): string {
|
|
|
|
return p.replace(/\\/g, '/')
|
|
|
|
}
|
2022-12-12 10:59:02 +00:00
|
|
|
|
|
|
|
declare module 'vite' {
|
|
|
|
export interface UserConfig {
|
|
|
|
/**
|
|
|
|
* special test only hook
|
|
|
|
*
|
|
|
|
* runs after build and before preview
|
|
|
|
*/
|
|
|
|
__test__?: () => void
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 19:49:24 +00:00
|
|
|
|
|
|
|
declare module 'vitest' {
|
|
|
|
export interface ProvidedContext {
|
|
|
|
wsEndpoint: string
|
|
|
|
}
|
|
|
|
}
|