From adc5c2e4a86e748d295430a3b618e1c9812852f4 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 26 Aug 2025 09:52:26 +0200 Subject: [PATCH] chore: speed up vite tests (#3258) --- packages/plugin-vite/tests/build_test.ts | 32 ++++++------------- packages/plugin-vite/tests/dev_server_test.ts | 24 ++++++++------ packages/plugin-vite/tests/test_utils.ts | 29 ++++++++++++----- 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/packages/plugin-vite/tests/build_test.ts b/packages/plugin-vite/tests/build_test.ts index 772bfa56..95d73f61 100644 --- a/packages/plugin-vite/tests/build_test.ts +++ b/packages/plugin-vite/tests/build_test.ts @@ -9,13 +9,13 @@ import { } from "./test_utils.ts"; import * as path from "@std/path"; +const viteResult = await buildVite(DEMO_DIR); + Deno.test({ name: "vite build - launches", fn: async () => { - await using res = await buildVite(DEMO_DIR); - await launchProd( - { cwd: res.tmp }, + { cwd: viteResult.tmp }, async (address) => { const res = await fetch(address); const text = await res.text(); @@ -30,8 +30,6 @@ Deno.test({ Deno.test({ name: "vite build - serves static files", fn: async () => { - await using viteResult = await buildVite(DEMO_DIR); - await launchProd( { cwd: viteResult.tmp }, async (address) => { @@ -48,10 +46,8 @@ Deno.test({ Deno.test({ name: "vite build - loads islands", fn: async () => { - await using res = await buildVite(DEMO_DIR); - await launchProd( - { cwd: res.tmp }, + { cwd: viteResult.tmp }, async (address) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/island_hooks`, { @@ -130,10 +126,8 @@ Deno.test({ Deno.test({ name: "vite build - load json inside npm package", fn: async () => { - await using res = await buildVite(DEMO_DIR); - await launchProd( - { cwd: res.tmp }, + { cwd: viteResult.tmp }, async (address) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/mime`, { @@ -152,10 +146,8 @@ Deno.test({ Deno.test({ name: "vite build - fetch static assets", fn: async () => { - await using res = await buildVite(DEMO_DIR); - await launchProd( - { cwd: res.tmp }, + { cwd: viteResult.tmp }, async (address) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/assets`, { @@ -242,10 +234,8 @@ Deno.test({ Deno.test({ name: "vite build - partial island", fn: async () => { - await using res = await buildVite(DEMO_DIR); - await launchProd( - { cwd: res.tmp }, + { cwd: viteResult.tmp }, async (address) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/partial`, { @@ -308,10 +298,8 @@ Deno.test({ Deno.test({ name: "vite build - import json from jsr dependency", fn: async () => { - await using res = await buildVite(DEMO_DIR); - await launchProd( - { cwd: res.tmp }, + { cwd: viteResult.tmp }, async (address) => { const res = await fetch(`${address}/tests/dep_json`); const json = await res.json(); @@ -326,10 +314,8 @@ Deno.test({ Deno.test({ name: "vite build - import node:*", fn: async () => { - await using res = await buildVite(DEMO_DIR); - await launchProd( - { cwd: res.tmp }, + { cwd: viteResult.tmp }, async (address) => { const res = await fetch(`${address}/tests/feed`); await res.body?.cancel(); diff --git a/packages/plugin-vite/tests/dev_server_test.ts b/packages/plugin-vite/tests/dev_server_test.ts index 0b09f7d5..ed111214 100644 --- a/packages/plugin-vite/tests/dev_server_test.ts +++ b/packages/plugin-vite/tests/dev_server_test.ts @@ -4,14 +4,18 @@ import { waitForText, withBrowser } from "../../fresh/tests/test_utils.tsx"; import { DEMO_DIR, FIXTURE_DIR, + launchDevServer, + prepareDevServer, updateFile, withDevServer, } from "./test_utils.ts"; +const tmp = await prepareDevServer(DEMO_DIR); + Deno.test({ name: "vite dev - launches", fn: async () => { - await withDevServer(DEMO_DIR, async (address) => { + await launchDevServer(tmp.dir, async (address) => { const res = await fetch(`${address}/tests/it_works`); const text = await res.text(); expect(text).toContain("it works"); @@ -24,7 +28,7 @@ Deno.test({ Deno.test({ name: "vite dev - serves static files", fn: async () => { - await withDevServer(DEMO_DIR, async (address) => { + await launchDevServer(tmp.dir, async (address) => { const res = await fetch(`${address}/test_static/foo.txt`); const text = await res.text(); expect(text).toContain("it works"); @@ -37,7 +41,7 @@ Deno.test({ Deno.test({ name: "vite dev - loads islands", fn: async () => { - await withDevServer(DEMO_DIR, async (address) => { + await launchDevServer(tmp.dir, async (address) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/island_hooks`, { waitUntil: "networkidle2", @@ -99,7 +103,7 @@ Deno.test({ name: "vite dev - can apply HMR to islands (hooks)", ignore: true, // Test is very flaky fn: async () => { - await withDevServer(DEMO_DIR, async (address, dir) => { + await launchDevServer(tmp.dir, async (address, dir) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/island_hooks`, { waitUntil: "networkidle2", @@ -132,7 +136,7 @@ Deno.test({ Deno.test({ name: "vite dev - can import json in npm package", fn: async () => { - await withDevServer(DEMO_DIR, async (address) => { + await launchDevServer(tmp.dir, async (address) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/mime`, { waitUntil: "networkidle2", @@ -148,7 +152,7 @@ Deno.test({ Deno.test({ name: "vite dev - inline env vars", fn: async () => { - await withDevServer(DEMO_DIR, async (address) => { + await launchDevServer(tmp.dir, async (address) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/env`, { waitUntil: "networkidle2", @@ -171,7 +175,7 @@ Deno.test({ Deno.test({ name: "vite dev - serves imported assets", fn: async () => { - await withDevServer(DEMO_DIR, async (address) => { + await launchDevServer(tmp.dir, async (address) => { const res = await fetch(`${address}/assets/deno-logo.png`); expect(res.status).toEqual(200); expect(res.headers.get("Content-Type")).toEqual("image/png"); @@ -220,7 +224,7 @@ Deno.test({ Deno.test({ name: "vite dev - partial island", fn: async () => { - await withDevServer(DEMO_DIR, async (address) => { + await launchDevServer(tmp.dir, async (address) => { await withBrowser(async (page) => { await page.goto(`${address}/tests/partial`, { waitUntil: "networkidle2", @@ -242,7 +246,7 @@ Deno.test({ Deno.test({ name: "vite dev - json from jsr dependency", fn: async () => { - await withDevServer(DEMO_DIR, async (address) => { + await launchDevServer(tmp.dir, async (address) => { const res = await fetch(`${address}/tests/dep_json`); const json = await res.json(); expect(json.name).toEqual("@marvinh-test/import-json"); @@ -255,7 +259,7 @@ Deno.test({ Deno.test({ name: "vite dev - import node:*", fn: async () => { - await withDevServer(DEMO_DIR, async (address) => { + await launchDevServer(tmp.dir, async (address) => { const res = await fetch(`${address}/tests/feed`); await res.body?.cancel(); expect(res.status).toEqual(200); diff --git a/packages/plugin-vite/tests/test_utils.ts b/packages/plugin-vite/tests/test_utils.ts index 5aed8fca..ff3602df 100644 --- a/packages/plugin-vite/tests/test_utils.ts +++ b/packages/plugin-vite/tests/test_utils.ts @@ -47,12 +47,8 @@ async function copyDir(from: string, to: string) { } } -export async function withDevServer( - fixtureDir: string, - fn: (address: string, dir: string) => void | Promise, - env: Record = {}, -) { - await using tmp = await withTmpDir({ +export async function prepareDevServer(fixtureDir: string) { + const tmp = await withTmpDir({ dir: path.join(import.meta.dirname!, ".."), prefix: "tmp_vite_", }); @@ -72,16 +68,33 @@ export default defineConfig({ `, ); + return tmp; +} + +export async function launchDevServer( + dir: string, + fn: (address: string, dir: string) => void | Promise, + env: Record = {}, +) { await withChildProcessServer( { - cwd: tmp.dir, + cwd: dir, args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"], env, }, - async (address) => await fn(address, tmp.dir), + async (address) => await fn(address, dir), ); } +export async function withDevServer( + fixtureDir: string, + fn: (address: string, dir: string) => void | Promise, + env: Record = {}, +) { + await using tmp = await prepareDevServer(fixtureDir); + await launchDevServer(tmp.dir, fn, env); +} + export async function buildVite(fixtureDir: string) { const tmp = await withTmpDir({ dir: path.join(import.meta.dirname!, ".."),