Skip to content

Commit 12afd80

Browse files
authored
Refactor playground tests to only create browser page if needed (#12360)
1 parent 4340c42 commit 12afd80

File tree

31 files changed

+131
-76
lines changed

31 files changed

+131
-76
lines changed
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
import { page, viteTestUrl } from "./index";
1+
import { viteTestUrl } from "./index";
22

33
export async function getTextResponse(path = "/"): Promise<string> {
44
const response = await getResponse(path);
55
return response.text();
66
}
77

8-
export async function getJsonResponse(
9-
path = "/"
10-
): Promise<null | Record<string, unknown> | Array<unknown>> {
8+
export async function getJsonResponse(path = "/"): Promise<unknown> {
119
const response = await getResponse(path);
12-
const text = await response.text();
13-
try {
14-
return JSON.parse(text);
15-
} catch {
16-
throw new Error("Invalid JSON response:\n" + text);
17-
}
10+
return response.json();
1811
}
1912

20-
export async function getResponse(path = "/") {
21-
const url = `${viteTestUrl}${path}`;
22-
const response = page.waitForResponse(url);
23-
await page.goto(url);
24-
return response;
13+
export async function getResponse(path = "/"): Promise<Response> {
14+
return fetch(`${viteTestUrl}${path}`);
2515
}

packages/vite-plugin-cloudflare/playground/additional-modules/__tests__/additional-modules.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
viteTestUrl,
77
} from "../../__test-utils__";
88

9+
export const browserMode = true;
10+
911
test("supports Data modules with a '.bin' extension", async () => {
1012
const result = await getJsonResponse("/bin");
1113
expect(result).toEqual({ byteLength: 342936 });

packages/vite-plugin-cloudflare/playground/assets/__tests__/assets.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { expect, test } from "vitest";
22
import { getResponse, page, viteTestUrl } from "../../__test-utils__";
33
import "./base-tests";
44

5+
export const browserMode = true;
6+
57
test("fetches transformed HTML asset", async () => {
68
await page.goto(`${viteTestUrl}/transformed-html-asset`);
79
const content = await page.textContent("h1");
@@ -10,8 +12,8 @@ test("fetches transformed HTML asset", async () => {
1012

1113
test("fetches original public directory asset if requested directly", async () => {
1214
const response = await getResponse("/public-image.svg");
13-
const contentType = await response.headerValue("content-type");
14-
const additionalHeader = await response.headerValue("additional-header");
15+
const contentType = response.headers.get("content-type");
16+
const additionalHeader = response.headers.get("additional-header");
1517
expect(contentType).toBe("image/svg+xml");
1618
expect(additionalHeader).toBe(null);
1719
});

packages/vite-plugin-cloudflare/playground/assets/__tests__/base-tests.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { getResponse, getTextResponse } from "../../__test-utils__";
33

44
test("fetches public directory asset", async () => {
55
const response = await getResponse("/public-directory-asset");
6-
const contentType = await response.headerValue("content-type");
7-
const additionalHeader = await response.headerValue("additional-header");
6+
const contentType = response.headers.get("content-type");
7+
const additionalHeader = response.headers.get("additional-header");
88
expect(contentType).toBe("image/svg+xml");
99
expect(additionalHeader).toBe("public-directory-asset");
1010
});
1111

1212
test("fetches imported asset", async () => {
1313
const response = await getResponse("/imported-asset");
14-
const contentType = await response.headerValue("content-type");
15-
const additionalHeader = await response.headerValue("additional-header");
14+
const contentType = response.headers.get("content-type");
15+
const additionalHeader = response.headers.get("additional-header");
1616
expect(contentType).toBe("image/svg+xml");
1717
expect(additionalHeader).toBe("imported-asset");
1818
});
@@ -24,8 +24,8 @@ test("fetches imported asset with url suffix", async () => {
2424

2525
test("fetches inline asset", async () => {
2626
const response = await getResponse("/inline-asset");
27-
const contentType = await response.headerValue("content-type");
28-
const additionalHeader = await response.headerValue("additional-header");
27+
const contentType = response.headers.get("content-type");
28+
const additionalHeader = response.headers.get("additional-header");
2929
expect(contentType).toBe("image/svg+xml");
3030
expect(additionalHeader).toBe("inline-asset");
3131
});

packages/vite-plugin-cloudflare/playground/assets/__tests__/public-dir-only/assets.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010

1111
test("fetches public directory asset", async () => {
1212
const response = await getResponse("/public-image.svg");
13-
const contentType = await response.headerValue("content-type");
13+
const contentType = response.headers.get("content-type");
1414
expect(contentType).toBe("image/svg+xml");
1515
});
1616

packages/vite-plugin-cloudflare/playground/custom-build-app/__tests__/custom-build-app.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
serverLogs,
77
} from "../../__test-utils__";
88

9+
export const browserMode = true;
10+
911
test("returns the index.html page", async () => {
1012
const content = await page.textContent("h1");
1113
expect(content).toBe("HTML page");

packages/vite-plugin-cloudflare/playground/errors/__tests__/errors.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, expect, test } from "vitest";
22
import { isBuild, page, viteTestUrl } from "../../__test-utils__";
33

4+
export const browserMode = true;
5+
46
describe.runIf(!isBuild)(
57
"error thrown in the default export of the entry Worker",
68
async () => {

packages/vite-plugin-cloudflare/playground/hot-channel/__tests__/hot-channel.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, expect, test } from "vitest";
22
import { isBuild, page, serverLogs, viteTestUrl } from "../../__test-utils__";
33

4+
export const browserMode = true;
5+
46
describe.runIf(!isBuild)("hot-channel", () => {
57
test("receives custom events sent from the dev server to the Worker", async () => {
68
await page.goto(viteTestUrl);

packages/vite-plugin-cloudflare/playground/importable-env/__tests__/importable-env.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { expect, test } from "vitest";
2-
import { getJsonResponse, serverLogs } from "../../__test-utils__";
2+
import { getJsonResponse, getResponse, serverLogs } from "../../__test-utils__";
33

44
test("the importable env is accessible from outside the request handler", async () => {
5+
await getResponse();
6+
57
expect(serverLogs.info.join()).toMatch(
68
/outside of request handler: importedEnv\["importable-env_VAR"\] === "my importable env variable"/
79
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
import "./base-tests";
2+
3+
export const browserMode = true;

0 commit comments

Comments
 (0)