import { resolve } from 'path' ; import execa from 'execa' ; import { Page } from 'playwright-chromium' ; export function slash ( p ) { return p . replace ( /\\/g , '/' ) ; } declare global { namespace NodeJS { interface Global { page ? : Page ; viteTestUrl ? : string ; } } } let rootDir ; let err ; let skipError ; const logs = ( ( global as any ) . browserLogs = [ ] ) ; const onConsole = ( msg ) => { logs . push ( msg . text ( ) ) ; } ; beforeAll ( async ( ) => { const page = global . page ; if ( ! page ) { return ; } skipError = false ; try { page . on ( 'console' , onConsole ) ; const testPath = expect . getState ( ) . testPath ; const testName = slash ( testPath ) . match ( /packages\/examples\/([\w-]+)\// ) ?. [ 1 ] ; if ( testName ) { rootDir = resolve ( __dirname , '../packages/temp' , testName ) ; if ( testName === 'vue2-demo' ) { await execa ( 'pnpm' , [ 'install' ] , { cwd : rootDir , stdio : 'inherit' } ) ; } execa ( 'pnpm' , [ 'run' , 'serve' ] , { cwd : rootDir , stdio : 'inherit' } ) ; await execa ( 'pnpm' , [ 'run' , 'build' ] , { cwd : rootDir , stdio : 'inherit' } ) ; const port = 5000 ; const url = ( global . viteTestUrl = `http://localhost:${ port }` ) ; await page . goto ( url ) ; } } catch ( e ) { if ( ! skipError ) { err = e ; } await page . close ( ) ; } } , 60000 ) ; afterAll ( async ( ) => { global . page ?. off ( 'console' , onConsole ) ; await global . page ?. close ( ) ; skipError = true ; await execa ( 'pnpm' , [ 'run' , 'stop' ] , { cwd : rootDir , stdio : 'inherit' } ) ; if ( err ) { throw err ; } } ) ;