chore: speed up vite tests (#3258)

This commit is contained in:
Marvin Hagemeister
2025-08-26 09:52:26 +02:00
committed by GitHub
parent a743b0c5d3
commit adc5c2e4a8
3 changed files with 44 additions and 41 deletions
+9 -23
View File
@@ -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();
+14 -10
View File
@@ -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);
+21 -8
View File
@@ -47,12 +47,8 @@ async function copyDir(from: string, to: string) {
}
}
export async function withDevServer(
fixtureDir: string,
fn: (address: string, dir: string) => void | Promise<void>,
env: Record<string, string> = {},
) {
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<void>,
env: Record<string, string> = {},
) {
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<void>,
env: Record<string, string> = {},
) {
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!, ".."),