chore: speed up vite tests (#3258)
This commit is contained in:
@@ -9,13 +9,13 @@ import {
|
|||||||
} from "./test_utils.ts";
|
} from "./test_utils.ts";
|
||||||
import * as path from "@std/path";
|
import * as path from "@std/path";
|
||||||
|
|
||||||
|
const viteResult = await buildVite(DEMO_DIR);
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - launches",
|
name: "vite build - launches",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using res = await buildVite(DEMO_DIR);
|
|
||||||
|
|
||||||
await launchProd(
|
await launchProd(
|
||||||
{ cwd: res.tmp },
|
{ cwd: viteResult.tmp },
|
||||||
async (address) => {
|
async (address) => {
|
||||||
const res = await fetch(address);
|
const res = await fetch(address);
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
@@ -30,8 +30,6 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - serves static files",
|
name: "vite build - serves static files",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using viteResult = await buildVite(DEMO_DIR);
|
|
||||||
|
|
||||||
await launchProd(
|
await launchProd(
|
||||||
{ cwd: viteResult.tmp },
|
{ cwd: viteResult.tmp },
|
||||||
async (address) => {
|
async (address) => {
|
||||||
@@ -48,10 +46,8 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - loads islands",
|
name: "vite build - loads islands",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using res = await buildVite(DEMO_DIR);
|
|
||||||
|
|
||||||
await launchProd(
|
await launchProd(
|
||||||
{ cwd: res.tmp },
|
{ cwd: viteResult.tmp },
|
||||||
async (address) => {
|
async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/island_hooks`, {
|
await page.goto(`${address}/tests/island_hooks`, {
|
||||||
@@ -130,10 +126,8 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - load json inside npm package",
|
name: "vite build - load json inside npm package",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using res = await buildVite(DEMO_DIR);
|
|
||||||
|
|
||||||
await launchProd(
|
await launchProd(
|
||||||
{ cwd: res.tmp },
|
{ cwd: viteResult.tmp },
|
||||||
async (address) => {
|
async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/mime`, {
|
await page.goto(`${address}/tests/mime`, {
|
||||||
@@ -152,10 +146,8 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - fetch static assets",
|
name: "vite build - fetch static assets",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using res = await buildVite(DEMO_DIR);
|
|
||||||
|
|
||||||
await launchProd(
|
await launchProd(
|
||||||
{ cwd: res.tmp },
|
{ cwd: viteResult.tmp },
|
||||||
async (address) => {
|
async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/assets`, {
|
await page.goto(`${address}/tests/assets`, {
|
||||||
@@ -242,10 +234,8 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - partial island",
|
name: "vite build - partial island",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using res = await buildVite(DEMO_DIR);
|
|
||||||
|
|
||||||
await launchProd(
|
await launchProd(
|
||||||
{ cwd: res.tmp },
|
{ cwd: viteResult.tmp },
|
||||||
async (address) => {
|
async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/partial`, {
|
await page.goto(`${address}/tests/partial`, {
|
||||||
@@ -308,10 +298,8 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - import json from jsr dependency",
|
name: "vite build - import json from jsr dependency",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using res = await buildVite(DEMO_DIR);
|
|
||||||
|
|
||||||
await launchProd(
|
await launchProd(
|
||||||
{ cwd: res.tmp },
|
{ cwd: viteResult.tmp },
|
||||||
async (address) => {
|
async (address) => {
|
||||||
const res = await fetch(`${address}/tests/dep_json`);
|
const res = await fetch(`${address}/tests/dep_json`);
|
||||||
const json = await res.json();
|
const json = await res.json();
|
||||||
@@ -326,10 +314,8 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - import node:*",
|
name: "vite build - import node:*",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await using res = await buildVite(DEMO_DIR);
|
|
||||||
|
|
||||||
await launchProd(
|
await launchProd(
|
||||||
{ cwd: res.tmp },
|
{ cwd: viteResult.tmp },
|
||||||
async (address) => {
|
async (address) => {
|
||||||
const res = await fetch(`${address}/tests/feed`);
|
const res = await fetch(`${address}/tests/feed`);
|
||||||
await res.body?.cancel();
|
await res.body?.cancel();
|
||||||
|
|||||||
@@ -4,14 +4,18 @@ import { waitForText, withBrowser } from "../../fresh/tests/test_utils.tsx";
|
|||||||
import {
|
import {
|
||||||
DEMO_DIR,
|
DEMO_DIR,
|
||||||
FIXTURE_DIR,
|
FIXTURE_DIR,
|
||||||
|
launchDevServer,
|
||||||
|
prepareDevServer,
|
||||||
updateFile,
|
updateFile,
|
||||||
withDevServer,
|
withDevServer,
|
||||||
} from "./test_utils.ts";
|
} from "./test_utils.ts";
|
||||||
|
|
||||||
|
const tmp = await prepareDevServer(DEMO_DIR);
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - launches",
|
name: "vite dev - launches",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address) => {
|
await launchDevServer(tmp.dir, async (address) => {
|
||||||
const res = await fetch(`${address}/tests/it_works`);
|
const res = await fetch(`${address}/tests/it_works`);
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
expect(text).toContain("it works");
|
expect(text).toContain("it works");
|
||||||
@@ -24,7 +28,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - serves static files",
|
name: "vite dev - serves static files",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address) => {
|
await launchDevServer(tmp.dir, async (address) => {
|
||||||
const res = await fetch(`${address}/test_static/foo.txt`);
|
const res = await fetch(`${address}/test_static/foo.txt`);
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
expect(text).toContain("it works");
|
expect(text).toContain("it works");
|
||||||
@@ -37,7 +41,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - loads islands",
|
name: "vite dev - loads islands",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address) => {
|
await launchDevServer(tmp.dir, async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/island_hooks`, {
|
await page.goto(`${address}/tests/island_hooks`, {
|
||||||
waitUntil: "networkidle2",
|
waitUntil: "networkidle2",
|
||||||
@@ -99,7 +103,7 @@ Deno.test({
|
|||||||
name: "vite dev - can apply HMR to islands (hooks)",
|
name: "vite dev - can apply HMR to islands (hooks)",
|
||||||
ignore: true, // Test is very flaky
|
ignore: true, // Test is very flaky
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address, dir) => {
|
await launchDevServer(tmp.dir, async (address, dir) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/island_hooks`, {
|
await page.goto(`${address}/tests/island_hooks`, {
|
||||||
waitUntil: "networkidle2",
|
waitUntil: "networkidle2",
|
||||||
@@ -132,7 +136,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - can import json in npm package",
|
name: "vite dev - can import json in npm package",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address) => {
|
await launchDevServer(tmp.dir, async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/mime`, {
|
await page.goto(`${address}/tests/mime`, {
|
||||||
waitUntil: "networkidle2",
|
waitUntil: "networkidle2",
|
||||||
@@ -148,7 +152,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - inline env vars",
|
name: "vite dev - inline env vars",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address) => {
|
await launchDevServer(tmp.dir, async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/env`, {
|
await page.goto(`${address}/tests/env`, {
|
||||||
waitUntil: "networkidle2",
|
waitUntil: "networkidle2",
|
||||||
@@ -171,7 +175,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - serves imported assets",
|
name: "vite dev - serves imported assets",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address) => {
|
await launchDevServer(tmp.dir, async (address) => {
|
||||||
const res = await fetch(`${address}/assets/deno-logo.png`);
|
const res = await fetch(`${address}/assets/deno-logo.png`);
|
||||||
expect(res.status).toEqual(200);
|
expect(res.status).toEqual(200);
|
||||||
expect(res.headers.get("Content-Type")).toEqual("image/png");
|
expect(res.headers.get("Content-Type")).toEqual("image/png");
|
||||||
@@ -220,7 +224,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - partial island",
|
name: "vite dev - partial island",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address) => {
|
await launchDevServer(tmp.dir, async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(`${address}/tests/partial`, {
|
await page.goto(`${address}/tests/partial`, {
|
||||||
waitUntil: "networkidle2",
|
waitUntil: "networkidle2",
|
||||||
@@ -242,7 +246,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - json from jsr dependency",
|
name: "vite dev - json from jsr dependency",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address) => {
|
await launchDevServer(tmp.dir, async (address) => {
|
||||||
const res = await fetch(`${address}/tests/dep_json`);
|
const res = await fetch(`${address}/tests/dep_json`);
|
||||||
const json = await res.json();
|
const json = await res.json();
|
||||||
expect(json.name).toEqual("@marvinh-test/import-json");
|
expect(json.name).toEqual("@marvinh-test/import-json");
|
||||||
@@ -255,7 +259,7 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - import node:*",
|
name: "vite dev - import node:*",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await withDevServer(DEMO_DIR, async (address) => {
|
await launchDevServer(tmp.dir, async (address) => {
|
||||||
const res = await fetch(`${address}/tests/feed`);
|
const res = await fetch(`${address}/tests/feed`);
|
||||||
await res.body?.cancel();
|
await res.body?.cancel();
|
||||||
expect(res.status).toEqual(200);
|
expect(res.status).toEqual(200);
|
||||||
|
|||||||
@@ -47,12 +47,8 @@ async function copyDir(from: string, to: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function withDevServer(
|
export async function prepareDevServer(fixtureDir: string) {
|
||||||
fixtureDir: string,
|
const tmp = await withTmpDir({
|
||||||
fn: (address: string, dir: string) => void | Promise<void>,
|
|
||||||
env: Record<string, string> = {},
|
|
||||||
) {
|
|
||||||
await using tmp = await withTmpDir({
|
|
||||||
dir: path.join(import.meta.dirname!, ".."),
|
dir: path.join(import.meta.dirname!, ".."),
|
||||||
prefix: "tmp_vite_",
|
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(
|
await withChildProcessServer(
|
||||||
{
|
{
|
||||||
cwd: tmp.dir,
|
cwd: dir,
|
||||||
args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"],
|
args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"],
|
||||||
env,
|
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) {
|
export async function buildVite(fixtureDir: string) {
|
||||||
const tmp = await withTmpDir({
|
const tmp = await withTmpDir({
|
||||||
dir: path.join(import.meta.dirname!, ".."),
|
dir: path.join(import.meta.dirname!, ".."),
|
||||||
|
|||||||
Reference in New Issue
Block a user