diff --git a/e2e/e2e.ts b/e2e/e2e.ts index e4b25de..cc17a93 100644 --- a/e2e/e2e.ts +++ b/e2e/e2e.ts @@ -1,6 +1,11 @@ /// /* istanbul ignore file */ +import path from "path"; +import url from "url"; +import fs from "fs"; +import type { AddressInfo } from "net"; + import { jest } from "@jest/globals"; import { firefox } from "playwright"; @@ -10,9 +15,7 @@ import vitePluginWasm from "../src/index.js"; import express from "express"; import waitPort from "wait-port"; import mime from "mime"; -import path from "path"; -import url from "url"; -import type { AddressInfo } from "net"; +import { temporaryDirectory } from "tempy"; const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); @@ -44,7 +47,7 @@ type VitePackages = }; async function buildAndStartProdServer( - viteVersion: number, + tempDir: string, vitePackages: VitePackages, transformTopLevelAwait: boolean, modernOnly: boolean @@ -54,9 +57,10 @@ async function buildAndStartProdServer( const result = await vite.build({ root: __dirname, build: { - target: "esnext" + target: "esnext", + outDir: path.resolve(tempDir, "dist") }, - cacheDir: path.resolve(__dirname, "node_modules", `.vite${viteVersion}`), + cacheDir: path.resolve(tempDir, ".vite"), plugins: [ ...(modernOnly ? [] : [vitePluginLegacy()]), vitePluginWasm(), @@ -105,13 +109,13 @@ async function buildAndStartProdServer( return `http://127.0.0.1:${port}/`; } -async function startDevServer(viteVersion: number, vitePackages: VitePackages): Promise { +async function startDevServer(tempDir: string, vitePackages: VitePackages): Promise { const { vite } = vitePackages; const devServer = await vite.createServer({ root: __dirname, plugins: [vitePluginWasm()], - cacheDir: path.resolve(__dirname, "node_modules", `.vite${viteVersion}`), + cacheDir: path.resolve(tempDir, ".vite"), logLevel: "error" }); @@ -134,14 +138,20 @@ async function createBrowser(modernBrowser: boolean) { } async function runTest( - viteVersion: number, vitePackages: VitePackages, devServer: boolean, transformTopLevelAwait: boolean, modernBrowser: boolean ) { + const tempDir = temporaryDirectory(); + process.on("exit", () => { + try { + fs.rmdirSync(tempDir, { recursive: true }); + } catch {} + }); + const server = await (devServer ? startDevServer : buildAndStartProdServer)( - viteVersion, + tempDir, vitePackages, transformTopLevelAwait, modernBrowser @@ -208,19 +218,19 @@ export function runTests(viteVersion: number, importVitePackages: () => Promise< } it(`vite ${viteVersion}: should work on modern browser in Vite dev server`, async () => { - await runTestWithRetry(viteVersion, await importVitePackages(), true, false, true); + await runTestWithRetry(await importVitePackages(), true, false, true); }); it(`vite ${viteVersion}: should work on modern browser without top-level await transform`, async () => { - await runTestWithRetry(viteVersion, await importVitePackages(), false, false, true); + await runTestWithRetry(await importVitePackages(), false, false, true); }); it(`vite ${viteVersion}: should work on modern browser with top-level await transform`, async () => { - await runTestWithRetry(viteVersion, await importVitePackages(), false, true, true); + await runTestWithRetry(await importVitePackages(), false, true, true); }); it(`vite ${viteVersion}: should work on legacy browser`, async () => { - await runTestWithRetry(viteVersion, await importVitePackages(), false, true, false); + await runTestWithRetry(await importVitePackages(), false, true, false); }); }); } diff --git a/e2e/package.json b/e2e/package.json index b3220cd..2086fdb 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -6,5 +6,8 @@ "private": true, "scripts": { "install": "yarn --cwd vite2 && yarn --cwd vite3 && yarn --cwd vite4 && yarn --cwd vite5 && yarn --cwd vite6" + }, + "dependencies": { + "tempy": "^3.1.0" } } diff --git a/e2e/yarn.lock b/e2e/yarn.lock index fb57ccd..320cf99 100644 --- a/e2e/yarn.lock +++ b/e2e/yarn.lock @@ -2,3 +2,46 @@ # yarn lockfile v1 +crypto-random-string@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-4.0.0.tgz#5a3cc53d7dd86183df5da0312816ceeeb5bb1fc2" + integrity sha512-x8dy3RnvYdlUcPOjkEHqozhiwzKNSq7GcPuXFbnyMOCHxX8V3OgIg/pYuabl2sbUPfIJaeAQB7PMOK8DFIdoRA== + dependencies: + type-fest "^1.0.1" + +is-stream@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-3.0.0.tgz#e6bfd7aa6bef69f4f472ce9bb681e3e57b4319ac" + integrity sha512-LnQR4bZ9IADDRSkvpqMGvt/tEJWclzklNgSw48V5EAaAeDd6qGvN8ei6k5p0tvxSR171VmGyHuTiAOfxAbr8kA== + +temp-dir@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-3.0.0.tgz#7f147b42ee41234cc6ba3138cd8e8aa2302acffa" + integrity sha512-nHc6S/bwIilKHNRgK/3jlhDoIHcp45YgyiwcAk46Tr0LfEqGBVpmiAyuiuxeVE44m3mXnEeVhaipLOEWmH+Njw== + +tempy@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/tempy/-/tempy-3.1.0.tgz#00958b6df85db8589cb595465e691852aac038e9" + integrity sha512-7jDLIdD2Zp0bDe5r3D2qtkd1QOCacylBuL7oa4udvN6v2pqr4+LcCr67C8DR1zkpaZ8XosF5m1yQSabKAW6f2g== + dependencies: + is-stream "^3.0.0" + temp-dir "^3.0.0" + type-fest "^2.12.2" + unique-string "^3.0.0" + +type-fest@^1.0.1: + version "1.4.0" + resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-1.4.0.tgz#e9fb813fe3bf1744ec359d55d1affefa76f14be1" + integrity sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA== + +type-fest@^2.12.2: + version "2.19.0" + resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.19.0.tgz#88068015bb33036a598b952e55e9311a60fd3a9b" + integrity sha512-RAH822pAdBgcNMAfWnCBU3CFZcfZ/i1eZjwFU/dsLKumyuuP3niueg2UAukXYF0E2AAoc82ZSSf9J0WQBinzHA== + +unique-string@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-3.0.0.tgz#84a1c377aff5fd7a8bc6b55d8244b2bd90d75b9a" + integrity sha512-VGXBUVwxKMBUznyffQweQABPRRW1vHZAbadFZud4pLFAqRGvv/96vafgjWFqzourzr8YonlQiPgH0YCJfawoGQ== + dependencies: + crypto-random-string "^4.0.0"