chore: refactor tests (#3243)

This commit is contained in:
Marvin Hagemeister
2025-08-22 12:14:19 +02:00
committed by GitHub
parent 873c54bd5f
commit 5c3fe27046
7 changed files with 252 additions and 459 deletions
Generated
+173 -377
View File
File diff suppressed because it is too large Load Diff
+5 -1
View File
@@ -274,7 +274,11 @@ Deno.test("migrates to last_shown property", async () => {
stdout: "piped", stdout: "piped",
}).output(); }).output();
const { stdout } = getStdOutput(out); const { stdout, stderr } = getStdOutput(out);
if (stdout === "") {
// deno-lint-ignore no-console
console.log(stderr);
}
expect(stdout).toMatch(/Fresh .* is available/); expect(stdout).toMatch(/Fresh .* is available/);
const checkFileAfter = JSON.parse( const checkFileAfter = JSON.parse(
+8 -6
View File
@@ -95,13 +95,15 @@ export async function withBrowser(fn: (page: Page) => void | Promise<void>) {
} }
} }
export interface TestChildServerOptions {
cwd: string;
args: string[];
bin?: string;
env?: Record<string, string>;
}
export async function withChildProcessServer( export async function withChildProcessServer(
options: { options: TestChildServerOptions,
cwd: string;
args: string[];
bin?: string;
env?: Record<string, string>;
},
fn: (address: string) => void | Promise<void>, fn: (address: string) => void | Promise<void>,
) { ) {
const aborter = new AbortController(); const aborter = new AbortController();
+1 -1
View File
@@ -30,7 +30,7 @@
"mime-db": "npm:mime-db@^1.54.0", "mime-db": "npm:mime-db@^1.54.0",
"preact": "npm:preact@^10.26.9", "preact": "npm:preact@^10.26.9",
"rollup-plugin-visualizer": "npm:rollup-plugin-visualizer@^6.0.3", "rollup-plugin-visualizer": "npm:rollup-plugin-visualizer@^6.0.3",
"vite": "npm:vite@^7.1.2", "vite": "npm:vite@^7.1.3",
"vite-plugin-inspect": "npm:vite-plugin-inspect@^11.3.2" "vite-plugin-inspect": "npm:vite-plugin-inspect@^11.3.2"
} }
} }
+5 -1
View File
@@ -32,10 +32,14 @@ export function deno(): Plugin {
}, },
async configResolved() { async configResolved() {
// TODO: Pass conditions // TODO: Pass conditions
ssrLoader = await new Workspace({}).createLoader(); ssrLoader = await new Workspace({
platform: "node",
cachedOnly: true,
}).createLoader();
browserLoader = await new Workspace({ browserLoader = await new Workspace({
platform: "browser", platform: "browser",
preserveJsx: true, preserveJsx: true,
cachedOnly: true,
}) })
.createLoader(); .createLoader();
}, },
+33 -72
View File
@@ -1,10 +1,12 @@
import { expect } from "@std/expect"; import { expect } from "@std/expect";
import { waitForText, withBrowser } from "../../fresh/tests/test_utils.tsx";
import { import {
waitForText, buildVite,
withBrowser, DEMO_DIR,
withChildProcessServer, FIXTURE_DIR,
} from "../../fresh/tests/test_utils.tsx"; launchProd,
import { buildVite, DEMO_DIR, FIXTURE_DIR, usingEnv } from "./test_utils.ts"; usingEnv,
} from "./test_utils.ts";
import * as path from "@std/path"; import * as path from "@std/path";
Deno.test({ Deno.test({
@@ -12,11 +14,8 @@ Deno.test({
fn: async () => { fn: async () => {
await using res = await buildVite(DEMO_DIR); await using res = await buildVite(DEMO_DIR);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
const res = await fetch(address); const res = await fetch(address);
const text = await res.text(); const text = await res.text();
@@ -33,11 +32,8 @@ Deno.test({
fn: async () => { fn: async () => {
await using viteResult = await buildVite(DEMO_DIR); await using viteResult = await buildVite(DEMO_DIR);
await withChildProcessServer( await launchProd(
{ { cwd: viteResult.tmp },
cwd: viteResult.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { 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();
@@ -54,11 +50,8 @@ Deno.test({
fn: async () => { fn: async () => {
await using res = await buildVite(DEMO_DIR); await using res = await buildVite(DEMO_DIR);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
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`, {
@@ -83,11 +76,8 @@ Deno.test({
const fixture = path.join(FIXTURE_DIR, "no_static"); const fixture = path.join(FIXTURE_DIR, "no_static");
await using res = await buildVite(fixture); await using res = await buildVite(fixture);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
const res = await fetch(`${address}/ok`); const res = await fetch(`${address}/ok`);
const text = await res.text(); const text = await res.text();
@@ -105,11 +95,8 @@ Deno.test({
const fixture = path.join(FIXTURE_DIR, "no_islands"); const fixture = path.join(FIXTURE_DIR, "no_islands");
await using res = await buildVite(fixture); await using res = await buildVite(fixture);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
const res = await fetch(`${address}`); const res = await fetch(`${address}`);
const text = await res.text(); const text = await res.text();
@@ -127,11 +114,8 @@ Deno.test({
const fixture = path.join(FIXTURE_DIR, "no_routes"); const fixture = path.join(FIXTURE_DIR, "no_routes");
await using res = await buildVite(fixture); await using res = await buildVite(fixture);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
const res = await fetch(`${address}`); const res = await fetch(`${address}`);
const text = await res.text(); const text = await res.text();
@@ -148,11 +132,8 @@ Deno.test({
fn: async () => { fn: async () => {
await using res = await buildVite(DEMO_DIR); await using res = await buildVite(DEMO_DIR);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
await withBrowser(async (page) => { await withBrowser(async (page) => {
await page.goto(`${address}/tests/mime`, { await page.goto(`${address}/tests/mime`, {
@@ -173,11 +154,8 @@ Deno.test({
fn: async () => { fn: async () => {
await using res = await buildVite(DEMO_DIR); await using res = await buildVite(DEMO_DIR);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
await withBrowser(async (page) => { await withBrowser(async (page) => {
await page.goto(`${address}/tests/assets`, { await page.goto(`${address}/tests/assets`, {
@@ -207,11 +185,8 @@ Deno.test({
const fixture = path.join(FIXTURE_DIR, "tailwind_no_app"); const fixture = path.join(FIXTURE_DIR, "tailwind_no_app");
await using res = await buildVite(fixture); await using res = await buildVite(fixture);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
await withBrowser(async (page) => { await withBrowser(async (page) => {
await page.goto(`${address}`, { await page.goto(`${address}`, {
@@ -240,11 +215,8 @@ Deno.test({
const fixture = path.join(FIXTURE_DIR, "tailwind_app"); const fixture = path.join(FIXTURE_DIR, "tailwind_app");
await using res = await buildVite(fixture); await using res = await buildVite(fixture);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
await withBrowser(async (page) => { await withBrowser(async (page) => {
await page.goto(`${address}`, { await page.goto(`${address}`, {
@@ -272,11 +244,8 @@ Deno.test({
fn: async () => { fn: async () => {
await using res = await buildVite(DEMO_DIR); await using res = await buildVite(DEMO_DIR);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
await withBrowser(async (page) => { await withBrowser(async (page) => {
await page.goto(`${address}/tests/partial`, { await page.goto(`${address}/tests/partial`, {
@@ -315,15 +284,10 @@ Deno.test({
] ]
) { ) {
using _ = usingEnv(key, revision); using _ = usingEnv(key, revision);
// deno-lint-ignore no-console
console.log("Checking...", key, Deno.env.get(key));
await using res = await buildVite(DEMO_DIR); await using res = await buildVite(DEMO_DIR);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
async (address) => { async (address) => {
const res = await fetch(`${address}/tests/build_id`); const res = await fetch(`${address}/tests/build_id`);
const text = await res.text(); const text = await res.text();
@@ -346,11 +310,8 @@ Deno.test({
fn: async () => { fn: async () => {
await using res = await buildVite(DEMO_DIR); await using res = await buildVite(DEMO_DIR);
await withChildProcessServer( await launchProd(
{ { cwd: res.tmp },
cwd: res.tmp,
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
},
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();
+27 -1
View File
@@ -73,7 +73,11 @@ export default defineConfig({
); );
await withChildProcessServer( await withChildProcessServer(
{ cwd: tmp.dir, args: ["run", "-A", "npm:vite", "--port", "0"], env }, {
cwd: tmp.dir,
args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"],
env,
},
async (address) => await fn(address, tmp.dir), async (address) => await fn(address, tmp.dir),
); );
} }
@@ -85,6 +89,7 @@ export async function buildVite(fixtureDir: string) {
}); });
const builder = await createBuilder({ const builder = await createBuilder({
logLevel: "error",
root: fixtureDir, root: fixtureDir,
build: { build: {
emptyOutDir: true, emptyOutDir: true,
@@ -125,3 +130,24 @@ export function usingEnv(name: string, value: string) {
}, },
}; };
} }
export interface ProdOptions {
cwd: string;
args?: string[];
bin?: string;
env?: Record<string, string>;
}
export async function launchProd(
options: ProdOptions,
fn: (address: string) => void | Promise<void>,
) {
return await withChildProcessServer(
{
cwd: options.cwd,
args: options.args ??
["serve", "-A", "--cached-only", "--port", "0", "_fresh/server.js"],
},
fn,
);
}