1
- import * as http from 'node:http'
1
+ import type * as http from 'node:http'
2
2
import path , { dirname , join , resolve } from 'node:path'
3
3
import os from 'node:os'
4
- import sirv from 'sirv'
5
4
import fs from 'fs-extra'
6
5
import { chromium } from 'playwright-chromium'
7
6
import type {
@@ -12,7 +11,13 @@ import type {
12
11
UserConfig ,
13
12
ViteDevServer
14
13
} from 'vite'
15
- import { build , createServer , loadConfigFromFile , mergeConfig } from 'vite'
14
+ import {
15
+ build ,
16
+ createServer ,
17
+ loadConfigFromFile ,
18
+ mergeConfig ,
19
+ preview
20
+ } from 'vite'
16
21
import type { Browser , Page } from 'playwright-chromium'
17
22
import type { RollupError , RollupWatcher , RollupWatcherEvent } from 'rollup'
18
23
import type { File } from 'vitest'
@@ -263,57 +268,18 @@ export async function startDefaultServe(): Promise<void> {
263
268
watcher = rollupOutput as RollupWatcher
264
269
await notifyRebuildComplete ( watcher )
265
270
}
266
- viteTestUrl = await startStaticServer ( testConfig )
267
- await page . goto ( viteTestUrl )
268
- }
269
- }
270
-
271
- function startStaticServer ( config : UserConfig ) : Promise < string > {
272
- // fallback internal base to ''
273
- let base = config ?. base
274
- if ( ! base || base === '/' || base === './' ) {
275
- base = ''
276
- }
277
-
278
- // @ts -ignore
279
- if ( config && config . __test__ ) {
280
271
// @ts -ignore
281
- config . __test__ ( )
282
- }
283
-
284
- // start static file server
285
- const serve = sirv ( resolve ( rootDir , 'dist' ) , {
286
- dev : ! ! config ?. build ?. watch
287
- } )
288
- // @ts -ignore
289
- const baseDir = config ?. testConfig ?. baseRoute
290
- const httpServer = ( server = http . createServer ( ( req , res ) => {
291
- if ( req . url === '/ping' ) {
292
- res . statusCode = 200
293
- res . end ( 'pong' )
294
- } else {
295
- if ( baseDir ) {
296
- req . url = path . posix . join ( baseDir , req . url )
297
- }
298
- serve ( req , res )
299
- }
300
- } ) )
301
- let port = 4173
302
- return new Promise ( ( resolve , reject ) => {
303
- const onError = ( e : any ) => {
304
- if ( e . code === 'EADDRINUSE' ) {
305
- httpServer . close ( )
306
- httpServer . listen ( ++ port )
307
- } else {
308
- reject ( e )
309
- }
272
+ if ( config && config . __test__ ) {
273
+ // @ts -ignore
274
+ config . __test__ ( )
310
275
}
311
- httpServer . on ( 'error' , onError )
312
- httpServer . listen ( port , ( ) => {
313
- httpServer . removeListener ( 'error' , onError )
314
- resolve ( `http://localhost:${ port } ${ base } ` )
315
- } )
316
- } )
276
+ const _nodeEnv = process . env . NODE_ENV
277
+ const previewServer = await preview ( testConfig )
278
+ // prevent preview change NODE_ENV
279
+ process . env . NODE_ENV = _nodeEnv
280
+ viteTestUrl = previewServer . resolvedUrls . local [ 0 ]
281
+ await page . goto ( viteTestUrl )
282
+ }
317
283
}
318
284
319
285
/**
0 commit comments