vite/playground/vitestGlobalSetup.ts

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

58 lines
1.9 KiB
TypeScript
Raw Normal View History

import os from 'node:os'
import path from 'node:path'
2022-05-11 09:03:19 +00:00
import fs from 'fs-extra'
2022-05-11 06:33:20 +00:00
import type { BrowserServer } from 'playwright-chromium'
import { chromium } from 'playwright-chromium'
import { hasWindowsUnicodeFsBug } from './hasWindowsUnicodeFsBug'
2022-05-11 06:33:20 +00:00
const DIR = path.join(os.tmpdir(), 'vitest_playwright_global_setup')
2022-05-11 06:33:20 +00:00
let browserServer: BrowserServer | undefined
export async function setup(): 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,
})
2020-12-20 03:37:53 +00:00
await fs.mkdirp(DIR)
await fs.writeFile(path.join(DIR, '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')
2022-05-11 06:33:20 +00:00
await fs.ensureDir(tempDir)
await fs.emptyDir(tempDir)
await fs
2022-05-09 07:36:14 +00:00
.copy(path.resolve(__dirname, '../playground'), tempDir, {
dereference: false,
filter(file) {
if (file.includes('中文-にほんご-한글-🌕🌖🌗')) {
return !hasWindowsUnicodeFsBug
}
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
}
})
}
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) {
fs.removeSync(path.resolve(__dirname, '../playground-temp'))
}
}