vite/playground/vitestGlobalSetup.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.2 KiB
TypeScript
Raw Normal View History

import fs from 'node:fs/promises'
import path from 'node:path'
import type { GlobalSetupContext } from 'vitest/node'
2022-05-11 06:33:20 +00:00
import type { BrowserServer } from 'playwright-chromium'
import { chromium } from 'playwright-chromium'
2022-05-11 06:33:20 +00:00
let browserServer: BrowserServer | undefined
export async function setup({ provide }: GlobalSetupContext): Promise<void> {
process.env.NODE_ENV = process.env.VITE_TEST_BUILD
? 'production'
: 'development'
2022-05-11 06:33:20 +00:00
browserServer = await chromium.launchServer({
2020-12-21 23:37:04 +00:00
headless: !process.env.VITE_DEBUG_SERVE,
args: process.env.CI
? ['--no-sandbox', '--disable-setuid-sandbox']
: undefined,
})
provide('wsEndpoint', browserServer.wsEndpoint())
2021-09-24 20:21:04 +00:00
2022-05-09 07:36:14 +00:00
const tempDir = path.resolve(__dirname, '../playground-temp')
await fs.rm(tempDir, { recursive: true, force: true })
await fs.mkdir(tempDir, { recursive: true })
await fs
.cp(path.resolve(__dirname, '../playground'), tempDir, {
recursive: true,
dereference: false,
filter(file) {
file = file.replace(/\\/g, '/')
return !file.includes('__tests__') && !/dist(?:\/|$)/.test(file)
},
})
.catch(async (error) => {
if (error.code === 'EPERM' && error.syscall === 'symlink') {
throw new Error(
'Could not create symlinks. On Windows, consider activating Developer Mode to allow non-admin users to create symlinks by following the instructions at https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development.',
)
} else {
throw error
}
})
// also setup dedicated copy for "variant" tests
for (const [original, variants] of [
['css', ['sass-modern', 'sass-modern-compiler']],
['css-sourcemap', ['sass-modern', 'sass-modern-compiler']],
] as const) {
for (const variant of variants) {
await fs.cp(
path.resolve(tempDir, original),
path.resolve(tempDir, `${original}__${variant}`),
{ recursive: true },
)
}
}
}
2022-05-11 06:33:20 +00:00
export async function teardown(): Promise<void> {
await browserServer?.close()
2022-05-11 06:33:20 +00:00
if (!process.env.VITE_PRESERVE_BUILD_ARTIFACTS) {
await fs.rm(path.resolve(__dirname, '../playground-temp'), {
recursive: true,
})
2022-05-11 06:33:20 +00:00
}
}