2022-06-19 19:59:52 +00:00
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'
2023-11-22 10:15:57 +00:00
import { hasWindowsUnicodeFsBug } from './hasWindowsUnicodeFsBug'
2020-12-18 20:54:14 +00:00
2022-05-11 06:33:20 +00:00
const DIR = path . join ( os . tmpdir ( ) , 'vitest_playwright_global_setup' )
2020-12-19 05:23:51 +00:00
2022-05-11 06:33:20 +00:00
let browserServer : BrowserServer | undefined
2022-06-13 17:34:46 +00:00
export async function setup ( ) : Promise < void > {
2022-11-22 14:25:10 +00:00
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 ,
2020-12-18 20:54:14 +00:00
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 )
2022-03-26 11:19:14 +00:00
await fs
2022-05-09 07:36:14 +00:00
. copy ( path . resolve ( __dirname , '../playground' ) , tempDir , {
2022-03-26 11:19:14 +00:00
dereference : false ,
filter ( file ) {
2023-11-22 10:15:57 +00:00
if ( file . includes ( '中文-にほんご-한글-🌕🌖🌗' ) ) {
return ! hasWindowsUnicodeFsBug
}
2022-03-26 11:19:14 +00:00
file = file . replace ( /\\/g , '/' )
2023-12-03 12:37:17 +00:00
return ! file . includes ( '__tests__' ) && ! /dist(?:\/|$)/ . test ( file )
2022-03-26 11:19:14 +00:00
} ,
} )
. 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
}
} )
2020-12-18 20:54:14 +00:00
}
2022-05-11 06:33:20 +00:00
2022-06-13 17:34:46 +00:00
export async function teardown ( ) : Promise < void > {
2022-09-06 16:57:10 +00:00
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' ) )
}
}