11/// <reference types="jest-extended" />
22/* istanbul ignore file */
33
4+ import path from "path" ;
5+ import url from "url" ;
6+ import fs from "fs" ;
7+ import type { AddressInfo } from "net" ;
8+
49import { jest } from "@jest/globals" ;
5- import { firefox } from "playwright" ;
10+ import { firefox , chromium } from "playwright" ;
611
7- // import type { RollupOutput } from "rollup";
8- type RollupOutput = any ;
12+ import type { RollupOutput } from "rollup" ;
913import vitePluginWasm from "../src/index.js" ;
1014
1115import express from "express" ;
1216import waitPort from "wait-port" ;
1317import mime from "mime" ;
14- import path from "path" ;
15- import url from "url" ;
16- import type { AddressInfo } from "net" ;
18+ import { temporaryDirectory } from "tempy" ;
1719
1820const __dirname = path . dirname ( url . fileURLToPath ( import . meta. url ) ) ;
1921
@@ -37,9 +39,15 @@ type VitePackages =
3739 vite : typeof import ( "./vite5/node_modules/vite" ) ;
3840 vitePluginLegacy : ( typeof import ( "./vite5/node_modules/@vitejs/plugin-legacy" ) ) [ "default" ] ;
3941 vitePluginTopLevelAwait : ( typeof import ( "./vite5/node_modules/vite-plugin-top-level-await" ) ) [ "default" ] ;
42+ }
43+ | {
44+ vite : typeof import ( "./vite6/node_modules/vite" ) ;
45+ vitePluginLegacy : ( typeof import ( "./vite6/node_modules/@vitejs/plugin-legacy" ) ) [ "default" ] ;
46+ vitePluginTopLevelAwait : ( typeof import ( "./vite6/node_modules/vite-plugin-top-level-await" ) ) [ "default" ] ;
4047 } ;
4148
4249async function buildAndStartProdServer (
50+ tempDir : string ,
4351 vitePackages : VitePackages ,
4452 transformTopLevelAwait : boolean ,
4553 modernOnly : boolean
@@ -49,8 +57,10 @@ async function buildAndStartProdServer(
4957 const result = await vite . build ( {
5058 root : __dirname ,
5159 build : {
52- target : "esnext"
60+ target : "esnext" ,
61+ outDir : path . resolve ( tempDir , "dist" )
5362 } ,
63+ cacheDir : path . resolve ( tempDir , ".vite" ) ,
5464 plugins : [
5565 ...( modernOnly ? [ ] : [ vitePluginLegacy ( ) ] ) ,
5666 vitePluginWasm ( ) ,
@@ -80,7 +90,7 @@ async function buildAndStartProdServer(
8090 if ( filePath in bundle ) {
8191 res . header ( "Access-Control-Allow-Origin" , "*" ) ;
8292 res . header ( "Access-Control-Allow-Methods" , "*" ) ;
83- const contentType = mime . getType ( filePath ) ;
93+ const contentType = mime . getType ( filePath ) || "application/octet-stream" ;
8494 const contentTypeWithEncoding = contentType + ( contentType . includes ( "text/" ) ? "; charset=utf-8" : "" ) ;
8595 res . contentType ( contentTypeWithEncoding ) ;
8696 res . send ( bundle [ filePath ] ) ;
@@ -99,12 +109,13 @@ async function buildAndStartProdServer(
99109 return `http://127.0.0.1:${ port } /` ;
100110}
101111
102- async function startDevServer ( vitePackages : VitePackages ) : Promise < string > {
112+ async function startDevServer ( tempDir : string , vitePackages : VitePackages ) : Promise < string > {
103113 const { vite } = vitePackages ;
104114
105115 const devServer = await vite . createServer ( {
106116 root : __dirname ,
107117 plugins : [ vitePluginWasm ( ) ] ,
118+ cacheDir : path . resolve ( tempDir , ".vite" ) ,
108119 logLevel : "error"
109120 } ) ;
110121
@@ -118,12 +129,15 @@ async function startDevServer(vitePackages: VitePackages): Promise<string> {
118129}
119130
120131async function createBrowser ( modernBrowser : boolean ) {
121- return await firefox . launch ( {
122- firefoxUserPrefs : {
123- // Simulate a legacy browser with ES modules support disabled
124- "dom.moduleScripts.enabled" : modernBrowser
125- }
126- } ) ;
132+ return modernBrowser
133+ ? await chromium . launch ( )
134+ : await firefox . launch ( {
135+ headless : false ,
136+ firefoxUserPrefs : {
137+ // Simulate a legacy browser with ES modules support disabled
138+ "dom.moduleScripts.enabled" : false
139+ }
140+ } ) ;
127141}
128142
129143async function runTest (
@@ -132,7 +146,15 @@ async function runTest(
132146 transformTopLevelAwait : boolean ,
133147 modernBrowser : boolean
134148) {
149+ const tempDir = temporaryDirectory ( ) ;
150+ process . on ( "exit" , ( ) => {
151+ try {
152+ fs . rmdirSync ( tempDir , { recursive : true } ) ;
153+ } catch { }
154+ } ) ;
155+
135156 const server = await ( devServer ? startDevServer : buildAndStartProdServer ) (
157+ tempDir ,
136158 vitePackages ,
137159 transformTopLevelAwait ,
138160 modernBrowser
@@ -189,7 +211,7 @@ const runTestWithRetry = async (...args: Parameters<typeof runTest>) => {
189211} ;
190212
191213export function runTests ( viteVersion : number , importVitePackages : ( ) => Promise < VitePackages > ) {
192- jest . setTimeout ( 60000 ) ;
214+ jest . setTimeout ( 600000 ) ;
193215
194216 describe ( `E2E test for Vite ${ viteVersion } ` , ( ) => {
195217 const nodeVersion = Number ( process . versions . node . split ( "." ) [ 0 ] ) ;
0 commit comments