2024-07-29 11:20:03 +00:00
import fs from 'node:fs/promises'
2022-06-19 19:59:52 +00:00
import path from 'node:path'
2024-01-16 19:49:24 +00:00
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'
2020-12-18 20:54:14 +00:00
2022-05-11 06:33:20 +00:00
let browserServer : BrowserServer | undefined
2024-01-16 19:49:24 +00:00
export async function setup ( { provide } : GlobalSetupContext ) : 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' ]
2022-12-04 07:19:06 +00:00
: undefined ,
2020-12-18 20:54:14 +00:00
} )
2024-01-16 19:49:24 +00:00
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' )
2024-07-29 11:20:03 +00:00
await fs . rm ( tempDir , { recursive : true , force : true } )
await fs . mkdir ( tempDir , { recursive : true } )
2022-03-26 11:19:14 +00:00
await fs
2024-07-29 11:20:03 +00:00
. cp ( path . resolve ( __dirname , '../playground' ) , tempDir , {
recursive : true ,
2022-03-26 11:19:14 +00:00
dereference : false ,
filter ( file ) {
file = file . replace ( /\\/g , '/' )
2023-12-03 12:37:17 +00:00
return ! file . includes ( '__tests__' ) && ! /dist(?:\/|$)/ . test ( file )
2022-12-04 07:19:06 +00:00
} ,
2022-03-26 11:19:14 +00:00
} )
. catch ( async ( error ) = > {
if ( error . code === 'EPERM' && error . syscall === 'symlink' ) {
throw new Error (
2022-12-04 07:19:06 +00:00
'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.' ,
2022-03-26 11:19:14 +00:00
)
} else {
throw error
}
} )
2024-07-30 05:35:18 +00:00
// also setup dedicated copy for "variant" tests
2024-09-01 15:27:03 +00:00
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 } ,
)
}
}
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 ) {
2024-07-29 11:20:03 +00:00
await fs . rm ( path . resolve ( __dirname , '../playground-temp' ) , {
recursive : true ,
} )
2022-05-11 06:33:20 +00:00
}
}