Skip to content

Commit 823cba8

Browse files
authored
Move the unenv tests from the preset to wrangler e2e (#10031)
1 parent 7fb0bfd commit 823cba8

File tree

12 files changed

+157
-87
lines changed

12 files changed

+157
-87
lines changed

.changeset/gold-beans-tie.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
"@cloudflare/unenv-preset": patch
4+
"wrangler": patch
5+
---
6+
7+
wrangler and vite-plugin now depend upon the latest version of unenv-preset

packages/unenv-preset/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ const { alias, inject, external, polyfill } = env;
1818
```
1919

2020
See the unenv [README](https://github.com/unjs/unenv/blob/main/README.md) for more details.
21+
22+
## Tests
23+
24+
This package is tested via wrangler e2e tests.

packages/unenv-preset/package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,14 @@
4040
"scripts": {
4141
"build": "unbuild",
4242
"check:lint": "eslint",
43-
"check:type": "tsc --noEmit",
44-
"test:ci": "vitest run",
45-
"test:watch": "vitest"
43+
"check:type": "tsc --noEmit"
4644
},
4745
"devDependencies": {
4846
"@types/debug": "4.1.12",
4947
"@types/node-unenv": "npm:@types/node@^22.14.0",
5048
"debug": "4.4.1",
5149
"typescript": "catalog:default",
52-
"unbuild": "^3.2.0",
53-
"undici": "catalog:default",
54-
"vitest": "catalog:default",
55-
"wrangler": "workspace:*"
50+
"unbuild": "^3.2.0"
5651
},
5752
"peerDependencies": {
5853
"unenv": "2.0.0-rc.17",

packages/unenv-preset/tests/index.test.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

packages/vite-plugin-cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"test:watch": "vitest"
4343
},
4444
"dependencies": {
45-
"@cloudflare/unenv-preset": "2.3.3",
45+
"@cloudflare/unenv-preset": "workspace:*",
4646
"@mjackson/node-fetch-server": "^0.6.1",
4747
"@rollup/plugin-replace": "^6.0.1",
4848
"get-port": "^7.1.0",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Test building and serving locally a worker in nodejs_compat mode.
3+
*/
4+
5+
import path from "node:path";
6+
import { fetch } from "undici";
7+
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
8+
import { formatCompatibilityDate } from "../../src/utils/compatibility-date";
9+
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
10+
import { generateResourceName } from "../helpers/generate-resource-name";
11+
import { TESTS } from "./worker/index";
12+
import type { WranglerLongLivedCommand } from "../helpers/wrangler";
13+
14+
describe(`@cloudflare/unenv-preset local tests`, () => {
15+
let url: string;
16+
let wrangler: WranglerLongLivedCommand;
17+
18+
beforeAll(async () => {
19+
const helper = new WranglerE2ETestHelper();
20+
await helper.seed({
21+
"wrangler.jsonc": JSON.stringify({
22+
name: generateResourceName(),
23+
main: path.join(__dirname, "/worker/index.ts"),
24+
compatibility_date: formatCompatibilityDate(new Date()),
25+
compatibility_flags: ["nodejs_compat"],
26+
vars: {
27+
DEBUG: "example",
28+
},
29+
}),
30+
});
31+
32+
wrangler = helper.runLongLived(`wrangler dev`, {
33+
stopOnTestFinished: false,
34+
});
35+
url = (await wrangler.waitForReady()).url;
36+
});
37+
38+
afterAll(async () => {
39+
await wrangler.stop();
40+
});
41+
42+
test.for(Object.keys(TESTS))("%s", { timeout: 20_000 }, async (testName) => {
43+
// Retries the callback until it succeeds or times out.
44+
// Useful for the i.e. DNS tests where underlying requests might error/timeout.
45+
await vi.waitFor(async () => {
46+
const response = await fetch(`${url}/${testName}`);
47+
const body = await response.text();
48+
expect(body).toMatch("OK!");
49+
});
50+
});
51+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Test building and serving a worker in nodejs_compat mode.
3+
* This makes sure the prod workerd is is compatible with unenv.
4+
*/
5+
6+
import assert from "node:assert";
7+
import path from "node:path";
8+
import { fetch } from "undici";
9+
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
10+
import { formatCompatibilityDate } from "../../src/utils/compatibility-date";
11+
import { CLOUDFLARE_ACCOUNT_ID } from "../helpers/account-id";
12+
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
13+
import { generateResourceName } from "../helpers/generate-resource-name";
14+
import { retry } from "../helpers/retry";
15+
import { TESTS } from "./worker/index";
16+
17+
describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
18+
`@cloudflare/unenv-preset remote tests`,
19+
() => {
20+
let url: string;
21+
let helper: WranglerE2ETestHelper;
22+
23+
beforeAll(async () => {
24+
helper = new WranglerE2ETestHelper();
25+
await helper.seed({
26+
"wrangler.jsonc": JSON.stringify({
27+
name: generateResourceName(),
28+
main: path.join(__dirname, "/worker/index.ts"),
29+
compatibility_date: formatCompatibilityDate(new Date()),
30+
compatibility_flags: ["nodejs_compat"],
31+
vars: {
32+
DEBUG: "example",
33+
},
34+
}),
35+
});
36+
37+
const { stdout } = await helper.run(`wrangler deploy`);
38+
39+
const match = stdout.match(
40+
/(?<url>https:\/\/tmp-e2e-.+?\..+?\.workers\.dev)/
41+
);
42+
assert(match?.groups);
43+
url = match.groups.url;
44+
45+
const { text } = await retry(
46+
(s) => s.status !== 200,
47+
async () => {
48+
const r = await fetch(`${url}/ping`);
49+
return { text: await r.text(), status: r.status };
50+
}
51+
);
52+
expect(text).toEqual("pong");
53+
}, 30_000);
54+
55+
afterAll(async () => {
56+
// clean up user Worker
57+
await helper.run(`wrangler delete`);
58+
});
59+
60+
test.for(Object.keys(TESTS))(
61+
"%s",
62+
{ timeout: 20_000 },
63+
async (testName) => {
64+
// Retries the callback until it succeeds or times out.
65+
// Useful for the i.e. DNS tests where underlying requests might error/timeout.
66+
await vi.waitFor(async () => {
67+
const response = await fetch(`${url}/${testName}`);
68+
const body = await response.text();
69+
expect(body).toMatch("OK!");
70+
});
71+
}
72+
);
73+
}
74+
);

packages/unenv-preset/tests/worker/index.ts renamed to packages/wrangler/e2e/unenv-preset/worker/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "node:assert";
22

33
// List all the test functions.
44
// The test can be executing by fetching the `/${testName}` url.
5-
export const TESTS = {
5+
export const TESTS: Record<string, () => void> = {
66
testCryptoGetRandomValues,
77
testImplementsBuffer,
88
testNodeCompatModules,
@@ -19,6 +19,10 @@ export default {
1919
async fetch(request: Request): Promise<Response> {
2020
const url = new URL(request.url);
2121
const testName = url.pathname.slice(1);
22+
if (testName === "ping") {
23+
// Used by the deploy test to know when the worker is online
24+
return new Response("pong");
25+
}
2226
const test = TESTS[testName];
2327
if (!test) {
2428
return new Response(
@@ -157,7 +161,9 @@ async function testTimers() {
157161
const timers = await import("node:timers");
158162
const timeout = timers.setTimeout(() => null, 1000);
159163
// active is deprecated and no more in the type
160-
(timers as any).active(timeout);
164+
(timers as unknown as { active: (t: NodeJS.Timeout) => void }).active(
165+
timeout
166+
);
161167
timers.clearTimeout(timeout);
162168

163169
const timersPromises = await import("node:timers/promises");
@@ -174,15 +180,17 @@ export async function testNet() {
174180
export async function testTls() {
175181
const tls = await import("node:tls");
176182
assert.strictEqual(typeof tls, "object");
183+
// @ts-expect-error Node types are wrong
177184
assert.strictEqual(typeof tls.convertALPNProtocols, "function");
178185
}
179186

180187
export async function testDebug() {
188+
// @ts-expect-error "@cloudflare/unenv-preset/npm/debug" is an unenv alias, it does not exist as a module.
181189
const debug = await import("@cloudflare/unenv-preset/npm/debug");
182190
const logs: string[] = [];
183191

184192
// Append all logs to the array instead of logging to console
185-
debug.default.log = (...args) =>
193+
debug.default.log = (...args: string[]) =>
186194
logs.push(args.map((arg) => arg.toString()).join(" "));
187195

188196
const exampleLog = debug.default("example");

packages/wrangler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
},
6868
"dependencies": {
6969
"@cloudflare/kv-asset-handler": "workspace:*",
70-
"@cloudflare/unenv-preset": "2.3.3",
70+
"@cloudflare/unenv-preset": "workspace:*",
7171
"blake3-wasm": "2.1.5",
7272
"esbuild": "catalog:default",
7373
"miniflare": "workspace:*",

0 commit comments

Comments
 (0)