Skip to content

Commit 5c3fe27

Browse files
chore: refactor tests (#3243)
1 parent 873c54b commit 5c3fe27

7 files changed

Lines changed: 252 additions & 459 deletions

File tree

deno.lock

Lines changed: 173 additions & 377 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/fresh/src/dev/update_check_test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,11 @@ Deno.test("migrates to last_shown property", async () => {
274274
stdout: "piped",
275275
}).output();
276276

277-
const { stdout } = getStdOutput(out);
277+
const { stdout, stderr } = getStdOutput(out);
278+
if (stdout === "") {
279+
// deno-lint-ignore no-console
280+
console.log(stderr);
281+
}
278282
expect(stdout).toMatch(/Fresh .* is available/);
279283

280284
const checkFileAfter = JSON.parse(

packages/fresh/tests/test_utils.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ export async function withBrowser(fn: (page: Page) => void | Promise<void>) {
9595
}
9696
}
9797

98+
export interface TestChildServerOptions {
99+
cwd: string;
100+
args: string[];
101+
bin?: string;
102+
env?: Record<string, string>;
103+
}
104+
98105
export async function withChildProcessServer(
99-
options: {
100-
cwd: string;
101-
args: string[];
102-
bin?: string;
103-
env?: Record<string, string>;
104-
},
106+
options: TestChildServerOptions,
105107
fn: (address: string) => void | Promise<void>,
106108
) {
107109
const aborter = new AbortController();

packages/plugin-vite/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"mime-db": "npm:mime-db@^1.54.0",
3131
"preact": "npm:preact@^10.26.9",
3232
"rollup-plugin-visualizer": "npm:rollup-plugin-visualizer@^6.0.3",
33-
"vite": "npm:vite@^7.1.2",
33+
"vite": "npm:vite@^7.1.3",
3434
"vite-plugin-inspect": "npm:vite-plugin-inspect@^11.3.2"
3535
}
3636
}

packages/plugin-vite/src/plugins/deno.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ export function deno(): Plugin {
3232
},
3333
async configResolved() {
3434
// TODO: Pass conditions
35-
ssrLoader = await new Workspace({}).createLoader();
35+
ssrLoader = await new Workspace({
36+
platform: "node",
37+
cachedOnly: true,
38+
}).createLoader();
3639
browserLoader = await new Workspace({
3740
platform: "browser",
3841
preserveJsx: true,
42+
cachedOnly: true,
3943
})
4044
.createLoader();
4145
},

packages/plugin-vite/tests/build_test.ts

Lines changed: 33 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { expect } from "@std/expect";
2+
import { waitForText, withBrowser } from "../../fresh/tests/test_utils.tsx";
23
import {
3-
waitForText,
4-
withBrowser,
5-
withChildProcessServer,
6-
} from "../../fresh/tests/test_utils.tsx";
7-
import { buildVite, DEMO_DIR, FIXTURE_DIR, usingEnv } from "./test_utils.ts";
4+
buildVite,
5+
DEMO_DIR,
6+
FIXTURE_DIR,
7+
launchProd,
8+
usingEnv,
9+
} from "./test_utils.ts";
810
import * as path from "@std/path";
911

1012
Deno.test({
1113
name: "vite build - launches",
1214
fn: async () => {
1315
await using res = await buildVite(DEMO_DIR);
1416

15-
await withChildProcessServer(
16-
{
17-
cwd: res.tmp,
18-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
19-
},
17+
await launchProd(
18+
{ cwd: res.tmp },
2019
async (address) => {
2120
const res = await fetch(address);
2221
const text = await res.text();
@@ -33,11 +32,8 @@ Deno.test({
3332
fn: async () => {
3433
await using viteResult = await buildVite(DEMO_DIR);
3534

36-
await withChildProcessServer(
37-
{
38-
cwd: viteResult.tmp,
39-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
40-
},
35+
await launchProd(
36+
{ cwd: viteResult.tmp },
4137
async (address) => {
4238
const res = await fetch(`${address}/test_static/foo.txt`);
4339
const text = await res.text();
@@ -54,11 +50,8 @@ Deno.test({
5450
fn: async () => {
5551
await using res = await buildVite(DEMO_DIR);
5652

57-
await withChildProcessServer(
58-
{
59-
cwd: res.tmp,
60-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
61-
},
53+
await launchProd(
54+
{ cwd: res.tmp },
6255
async (address) => {
6356
await withBrowser(async (page) => {
6457
await page.goto(`${address}/tests/island_hooks`, {
@@ -83,11 +76,8 @@ Deno.test({
8376
const fixture = path.join(FIXTURE_DIR, "no_static");
8477
await using res = await buildVite(fixture);
8578

86-
await withChildProcessServer(
87-
{
88-
cwd: res.tmp,
89-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
90-
},
79+
await launchProd(
80+
{ cwd: res.tmp },
9181
async (address) => {
9282
const res = await fetch(`${address}/ok`);
9383
const text = await res.text();
@@ -105,11 +95,8 @@ Deno.test({
10595
const fixture = path.join(FIXTURE_DIR, "no_islands");
10696
await using res = await buildVite(fixture);
10797

108-
await withChildProcessServer(
109-
{
110-
cwd: res.tmp,
111-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
112-
},
98+
await launchProd(
99+
{ cwd: res.tmp },
113100
async (address) => {
114101
const res = await fetch(`${address}`);
115102
const text = await res.text();
@@ -127,11 +114,8 @@ Deno.test({
127114
const fixture = path.join(FIXTURE_DIR, "no_routes");
128115
await using res = await buildVite(fixture);
129116

130-
await withChildProcessServer(
131-
{
132-
cwd: res.tmp,
133-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
134-
},
117+
await launchProd(
118+
{ cwd: res.tmp },
135119
async (address) => {
136120
const res = await fetch(`${address}`);
137121
const text = await res.text();
@@ -148,11 +132,8 @@ Deno.test({
148132
fn: async () => {
149133
await using res = await buildVite(DEMO_DIR);
150134

151-
await withChildProcessServer(
152-
{
153-
cwd: res.tmp,
154-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
155-
},
135+
await launchProd(
136+
{ cwd: res.tmp },
156137
async (address) => {
157138
await withBrowser(async (page) => {
158139
await page.goto(`${address}/tests/mime`, {
@@ -173,11 +154,8 @@ Deno.test({
173154
fn: async () => {
174155
await using res = await buildVite(DEMO_DIR);
175156

176-
await withChildProcessServer(
177-
{
178-
cwd: res.tmp,
179-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
180-
},
157+
await launchProd(
158+
{ cwd: res.tmp },
181159
async (address) => {
182160
await withBrowser(async (page) => {
183161
await page.goto(`${address}/tests/assets`, {
@@ -207,11 +185,8 @@ Deno.test({
207185
const fixture = path.join(FIXTURE_DIR, "tailwind_no_app");
208186
await using res = await buildVite(fixture);
209187

210-
await withChildProcessServer(
211-
{
212-
cwd: res.tmp,
213-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
214-
},
188+
await launchProd(
189+
{ cwd: res.tmp },
215190
async (address) => {
216191
await withBrowser(async (page) => {
217192
await page.goto(`${address}`, {
@@ -240,11 +215,8 @@ Deno.test({
240215
const fixture = path.join(FIXTURE_DIR, "tailwind_app");
241216
await using res = await buildVite(fixture);
242217

243-
await withChildProcessServer(
244-
{
245-
cwd: res.tmp,
246-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
247-
},
218+
await launchProd(
219+
{ cwd: res.tmp },
248220
async (address) => {
249221
await withBrowser(async (page) => {
250222
await page.goto(`${address}`, {
@@ -272,11 +244,8 @@ Deno.test({
272244
fn: async () => {
273245
await using res = await buildVite(DEMO_DIR);
274246

275-
await withChildProcessServer(
276-
{
277-
cwd: res.tmp,
278-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
279-
},
247+
await launchProd(
248+
{ cwd: res.tmp },
280249
async (address) => {
281250
await withBrowser(async (page) => {
282251
await page.goto(`${address}/tests/partial`, {
@@ -315,15 +284,10 @@ Deno.test({
315284
]
316285
) {
317286
using _ = usingEnv(key, revision);
318-
// deno-lint-ignore no-console
319-
console.log("Checking...", key, Deno.env.get(key));
320287
await using res = await buildVite(DEMO_DIR);
321288

322-
await withChildProcessServer(
323-
{
324-
cwd: res.tmp,
325-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
326-
},
289+
await launchProd(
290+
{ cwd: res.tmp },
327291
async (address) => {
328292
const res = await fetch(`${address}/tests/build_id`);
329293
const text = await res.text();
@@ -346,11 +310,8 @@ Deno.test({
346310
fn: async () => {
347311
await using res = await buildVite(DEMO_DIR);
348312

349-
await withChildProcessServer(
350-
{
351-
cwd: res.tmp,
352-
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
353-
},
313+
await launchProd(
314+
{ cwd: res.tmp },
354315
async (address) => {
355316
const res = await fetch(`${address}/tests/dep_json`);
356317
const json = await res.json();

packages/plugin-vite/tests/test_utils.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ export default defineConfig({
7373
);
7474

7575
await withChildProcessServer(
76-
{ cwd: tmp.dir, args: ["run", "-A", "npm:vite", "--port", "0"], env },
76+
{
77+
cwd: tmp.dir,
78+
args: ["run", "-A", "--cached-only", "npm:vite", "--port", "0"],
79+
env,
80+
},
7781
async (address) => await fn(address, tmp.dir),
7882
);
7983
}
@@ -85,6 +89,7 @@ export async function buildVite(fixtureDir: string) {
8589
});
8690

8791
const builder = await createBuilder({
92+
logLevel: "error",
8893
root: fixtureDir,
8994
build: {
9095
emptyOutDir: true,
@@ -125,3 +130,24 @@ export function usingEnv(name: string, value: string) {
125130
},
126131
};
127132
}
133+
134+
export interface ProdOptions {
135+
cwd: string;
136+
args?: string[];
137+
bin?: string;
138+
env?: Record<string, string>;
139+
}
140+
141+
export async function launchProd(
142+
options: ProdOptions,
143+
fn: (address: string) => void | Promise<void>,
144+
) {
145+
return await withChildProcessServer(
146+
{
147+
cwd: options.cwd,
148+
args: options.args ??
149+
["serve", "-A", "--cached-only", "--port", "0", "_fresh/server.js"],
150+
},
151+
fn,
152+
);
153+
}

0 commit comments

Comments
 (0)