Skip to content

Commit adc5c2e

Browse files
chore: speed up vite tests (#3258)
1 parent a743b0c commit adc5c2e

3 files changed

Lines changed: 44 additions & 41 deletions

File tree

packages/plugin-vite/tests/build_test.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
} from "./test_utils.ts";
1010
import * as path from "@std/path";
1111

12+
const viteResult = await buildVite(DEMO_DIR);
13+
1214
Deno.test({
1315
name: "vite build - launches",
1416
fn: async () => {
15-
await using res = await buildVite(DEMO_DIR);
16-
1717
await launchProd(
18-
{ cwd: res.tmp },
18+
{ cwd: viteResult.tmp },
1919
async (address) => {
2020
const res = await fetch(address);
2121
const text = await res.text();
@@ -30,8 +30,6 @@ Deno.test({
3030
Deno.test({
3131
name: "vite build - serves static files",
3232
fn: async () => {
33-
await using viteResult = await buildVite(DEMO_DIR);
34-
3533
await launchProd(
3634
{ cwd: viteResult.tmp },
3735
async (address) => {
@@ -48,10 +46,8 @@ Deno.test({
4846
Deno.test({
4947
name: "vite build - loads islands",
5048
fn: async () => {
51-
await using res = await buildVite(DEMO_DIR);
52-
5349
await launchProd(
54-
{ cwd: res.tmp },
50+
{ cwd: viteResult.tmp },
5551
async (address) => {
5652
await withBrowser(async (page) => {
5753
await page.goto(`${address}/tests/island_hooks`, {
@@ -130,10 +126,8 @@ Deno.test({
130126
Deno.test({
131127
name: "vite build - load json inside npm package",
132128
fn: async () => {
133-
await using res = await buildVite(DEMO_DIR);
134-
135129
await launchProd(
136-
{ cwd: res.tmp },
130+
{ cwd: viteResult.tmp },
137131
async (address) => {
138132
await withBrowser(async (page) => {
139133
await page.goto(`${address}/tests/mime`, {
@@ -152,10 +146,8 @@ Deno.test({
152146
Deno.test({
153147
name: "vite build - fetch static assets",
154148
fn: async () => {
155-
await using res = await buildVite(DEMO_DIR);
156-
157149
await launchProd(
158-
{ cwd: res.tmp },
150+
{ cwd: viteResult.tmp },
159151
async (address) => {
160152
await withBrowser(async (page) => {
161153
await page.goto(`${address}/tests/assets`, {
@@ -242,10 +234,8 @@ Deno.test({
242234
Deno.test({
243235
name: "vite build - partial island",
244236
fn: async () => {
245-
await using res = await buildVite(DEMO_DIR);
246-
247237
await launchProd(
248-
{ cwd: res.tmp },
238+
{ cwd: viteResult.tmp },
249239
async (address) => {
250240
await withBrowser(async (page) => {
251241
await page.goto(`${address}/tests/partial`, {
@@ -308,10 +298,8 @@ Deno.test({
308298
Deno.test({
309299
name: "vite build - import json from jsr dependency",
310300
fn: async () => {
311-
await using res = await buildVite(DEMO_DIR);
312-
313301
await launchProd(
314-
{ cwd: res.tmp },
302+
{ cwd: viteResult.tmp },
315303
async (address) => {
316304
const res = await fetch(`${address}/tests/dep_json`);
317305
const json = await res.json();
@@ -326,10 +314,8 @@ Deno.test({
326314
Deno.test({
327315
name: "vite build - import node:*",
328316
fn: async () => {
329-
await using res = await buildVite(DEMO_DIR);
330-
331317
await launchProd(
332-
{ cwd: res.tmp },
318+
{ cwd: viteResult.tmp },
333319
async (address) => {
334320
const res = await fetch(`${address}/tests/feed`);
335321
await res.body?.cancel();

packages/plugin-vite/tests/dev_server_test.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import { waitForText, withBrowser } from "../../fresh/tests/test_utils.tsx";
44
import {
55
DEMO_DIR,
66
FIXTURE_DIR,
7+
launchDevServer,
8+
prepareDevServer,
79
updateFile,
810
withDevServer,
911
} from "./test_utils.ts";
1012

13+
const tmp = await prepareDevServer(DEMO_DIR);
14+
1115
Deno.test({
1216
name: "vite dev - launches",
1317
fn: async () => {
14-
await withDevServer(DEMO_DIR, async (address) => {
18+
await launchDevServer(tmp.dir, async (address) => {
1519
const res = await fetch(`${address}/tests/it_works`);
1620
const text = await res.text();
1721
expect(text).toContain("it works");
@@ -24,7 +28,7 @@ Deno.test({
2428
Deno.test({
2529
name: "vite dev - serves static files",
2630
fn: async () => {
27-
await withDevServer(DEMO_DIR, async (address) => {
31+
await launchDevServer(tmp.dir, async (address) => {
2832
const res = await fetch(`${address}/test_static/foo.txt`);
2933
const text = await res.text();
3034
expect(text).toContain("it works");
@@ -37,7 +41,7 @@ Deno.test({
3741
Deno.test({
3842
name: "vite dev - loads islands",
3943
fn: async () => {
40-
await withDevServer(DEMO_DIR, async (address) => {
44+
await launchDevServer(tmp.dir, async (address) => {
4145
await withBrowser(async (page) => {
4246
await page.goto(`${address}/tests/island_hooks`, {
4347
waitUntil: "networkidle2",
@@ -99,7 +103,7 @@ Deno.test({
99103
name: "vite dev - can apply HMR to islands (hooks)",
100104
ignore: true, // Test is very flaky
101105
fn: async () => {
102-
await withDevServer(DEMO_DIR, async (address, dir) => {
106+
await launchDevServer(tmp.dir, async (address, dir) => {
103107
await withBrowser(async (page) => {
104108
await page.goto(`${address}/tests/island_hooks`, {
105109
waitUntil: "networkidle2",
@@ -132,7 +136,7 @@ Deno.test({
132136
Deno.test({
133137
name: "vite dev - can import json in npm package",
134138
fn: async () => {
135-
await withDevServer(DEMO_DIR, async (address) => {
139+
await launchDevServer(tmp.dir, async (address) => {
136140
await withBrowser(async (page) => {
137141
await page.goto(`${address}/tests/mime`, {
138142
waitUntil: "networkidle2",
@@ -148,7 +152,7 @@ Deno.test({
148152
Deno.test({
149153
name: "vite dev - inline env vars",
150154
fn: async () => {
151-
await withDevServer(DEMO_DIR, async (address) => {
155+
await launchDevServer(tmp.dir, async (address) => {
152156
await withBrowser(async (page) => {
153157
await page.goto(`${address}/tests/env`, {
154158
waitUntil: "networkidle2",
@@ -171,7 +175,7 @@ Deno.test({
171175
Deno.test({
172176
name: "vite dev - serves imported assets",
173177
fn: async () => {
174-
await withDevServer(DEMO_DIR, async (address) => {
178+
await launchDevServer(tmp.dir, async (address) => {
175179
const res = await fetch(`${address}/assets/deno-logo.png`);
176180
expect(res.status).toEqual(200);
177181
expect(res.headers.get("Content-Type")).toEqual("image/png");
@@ -220,7 +224,7 @@ Deno.test({
220224
Deno.test({
221225
name: "vite dev - partial island",
222226
fn: async () => {
223-
await withDevServer(DEMO_DIR, async (address) => {
227+
await launchDevServer(tmp.dir, async (address) => {
224228
await withBrowser(async (page) => {
225229
await page.goto(`${address}/tests/partial`, {
226230
waitUntil: "networkidle2",
@@ -242,7 +246,7 @@ Deno.test({
242246
Deno.test({
243247
name: "vite dev - json from jsr dependency",
244248
fn: async () => {
245-
await withDevServer(DEMO_DIR, async (address) => {
249+
await launchDevServer(tmp.dir, async (address) => {
246250
const res = await fetch(`${address}/tests/dep_json`);
247251
const json = await res.json();
248252
expect(json.name).toEqual("@marvinh-test/import-json");
@@ -255,7 +259,7 @@ Deno.test({
255259
Deno.test({
256260
name: "vite dev - import node:*",
257261
fn: async () => {
258-
await withDevServer(DEMO_DIR, async (address) => {
262+
await launchDevServer(tmp.dir, async (address) => {
259263
const res = await fetch(`${address}/tests/feed`);
260264
await res.body?.cancel();
261265
expect(res.status).toEqual(200);

packages/plugin-vite/tests/test_utils.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@ async function copyDir(from: string, to: string) {
4747
}
4848
}
4949

50-
export async function withDevServer(
51-
fixtureDir: string,
52-
fn: (address: string, dir: string) => void | Promise<void>,
53-
env: Record<string, string> = {},
54-
) {
55-
await using tmp = await withTmpDir({
50+
export async function prepareDevServer(fixtureDir: string) {
51+
const tmp = await withTmpDir({
5652
dir: path.join(import.meta.dirname!, ".."),
5753
prefix: "tmp_vite_",
5854
});
@@ -72,16 +68,33 @@ export default defineConfig({
7268
`,
7369
);
7470

71+
return tmp;
72+
}
73+
74+
export async function launchDevServer(
75+
dir: string,
76+
fn: (address: string, dir: string) => void | Promise<void>,
77+
env: Record<string, string> = {},
78+
) {
7579
await withChildProcessServer(
7680
{
77-
cwd: tmp.dir,
81+
cwd: dir,
7882
args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"],
7983
env,
8084
},
81-
async (address) => await fn(address, tmp.dir),
85+
async (address) => await fn(address, dir),
8286
);
8387
}
8488

89+
export async function withDevServer(
90+
fixtureDir: string,
91+
fn: (address: string, dir: string) => void | Promise<void>,
92+
env: Record<string, string> = {},
93+
) {
94+
await using tmp = await prepareDevServer(fixtureDir);
95+
await launchDevServer(tmp.dir, fn, env);
96+
}
97+
8598
export async function buildVite(fixtureDir: string) {
8699
const tmp = await withTmpDir({
87100
dir: path.join(import.meta.dirname!, ".."),

0 commit comments

Comments
 (0)