Skip to content

Move the unenv tests from the preset to wrangler e2e #10031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/gold-beans-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@cloudflare/vite-plugin": patch
"@cloudflare/unenv-preset": patch
"wrangler": patch
---

wrangler and vite-plugin now depend upon the latest version of unenv-preset
4 changes: 4 additions & 0 deletions packages/unenv-preset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ const { alias, inject, external, polyfill } = env;
```

See the unenv [README](https://github.com/unjs/unenv/blob/main/README.md) for more details.

## Tests

This package is tested via wrangler e2e tests.
9 changes: 2 additions & 7 deletions packages/unenv-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,14 @@
"scripts": {
"build": "unbuild",
"check:lint": "eslint",
"check:type": "tsc --noEmit",
"test:ci": "vitest run",
"test:watch": "vitest"
"check:type": "tsc --noEmit"
},
"devDependencies": {
"@types/debug": "4.1.12",
"@types/node-unenv": "npm:@types/node@^22.14.0",
"debug": "4.4.1",
"typescript": "catalog:default",
"unbuild": "^3.2.0",
"undici": "catalog:default",
"vitest": "catalog:default",
"wrangler": "workspace:*"
"unbuild": "^3.2.0"
},
"peerDependencies": {
"unenv": "2.0.0-rc.17",
Expand Down
46 changes: 0 additions & 46 deletions packages/unenv-preset/tests/index.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/vite-plugin-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"test:watch": "vitest"
},
"dependencies": {
"@cloudflare/unenv-preset": "2.3.3",
"@cloudflare/unenv-preset": "workspace:*",
"@mjackson/node-fetch-server": "^0.6.1",
"@rollup/plugin-replace": "^6.0.1",
"get-port": "^7.1.0",
Expand Down
51 changes: 51 additions & 0 deletions packages/wrangler/e2e/unenv-preset/local.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Test building and serving locally a worker in nodejs_compat mode.
*/

import path from "node:path";
import { fetch } from "undici";
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import { formatCompatibilityDate } from "../../src/utils/compatibility-date";
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
import { generateResourceName } from "../helpers/generate-resource-name";
import { TESTS } from "./worker/index";
import type { WranglerLongLivedCommand } from "../helpers/wrangler";

describe(`@cloudflare/unenv-preset local tests`, () => {
let url: string;
let wrangler: WranglerLongLivedCommand;

beforeAll(async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed({
"wrangler.jsonc": JSON.stringify({
name: generateResourceName(),
main: path.join(__dirname, "/worker/index.ts"),
compatibility_date: formatCompatibilityDate(new Date()),
compatibility_flags: ["nodejs_compat"],
vars: {
DEBUG: "example",
},
}),
});

wrangler = helper.runLongLived(`wrangler dev`, {
stopOnTestFinished: false,
});
url = (await wrangler.waitForReady()).url;
});

afterAll(async () => {
await wrangler.stop();
});

test.for(Object.keys(TESTS))("%s", { timeout: 20_000 }, async (testName) => {
// Retries the callback until it succeeds or times out.
// Useful for the i.e. DNS tests where underlying requests might error/timeout.
await vi.waitFor(async () => {
const response = await fetch(`${url}/${testName}`);
const body = await response.text();
expect(body).toMatch("OK!");
});
});
});
74 changes: 74 additions & 0 deletions packages/wrangler/e2e/unenv-preset/remote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Test building and serving a worker in nodejs_compat mode.
* This makes sure the prod workerd is is compatible with unenv.
*/

import assert from "node:assert";
import path from "node:path";
import { fetch } from "undici";
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import { formatCompatibilityDate } from "../../src/utils/compatibility-date";
import { CLOUDFLARE_ACCOUNT_ID } from "../helpers/account-id";
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
import { generateResourceName } from "../helpers/generate-resource-name";
import { retry } from "../helpers/retry";
import { TESTS } from "./worker/index";

describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
`@cloudflare/unenv-preset remote tests`,
() => {
let url: string;
let helper: WranglerE2ETestHelper;

beforeAll(async () => {
helper = new WranglerE2ETestHelper();
await helper.seed({
"wrangler.jsonc": JSON.stringify({
name: generateResourceName(),
main: path.join(__dirname, "/worker/index.ts"),
compatibility_date: formatCompatibilityDate(new Date()),
compatibility_flags: ["nodejs_compat"],
vars: {
DEBUG: "example",
},
}),
});

const { stdout } = await helper.run(`wrangler deploy`);

const match = stdout.match(
/(?<url>https:\/\/tmp-e2e-.+?\..+?\.workers\.dev)/
);
assert(match?.groups);
url = match.groups.url;

const { text } = await retry(
(s) => s.status !== 200,
async () => {
const r = await fetch(`${url}/ping`);
return { text: await r.text(), status: r.status };
}
);
expect(text).toEqual("pong");
}, 30_000);

afterAll(async () => {
// clean up user Worker
await helper.run(`wrangler delete`);
});

test.for(Object.keys(TESTS))(
"%s",
{ timeout: 20_000 },
async (testName) => {
// Retries the callback until it succeeds or times out.
// Useful for the i.e. DNS tests where underlying requests might error/timeout.
await vi.waitFor(async () => {
const response = await fetch(`${url}/${testName}`);
const body = await response.text();
expect(body).toMatch("OK!");
});
}
);
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "node:assert";

// List all the test functions.
// The test can be executing by fetching the `/${testName}` url.
export const TESTS = {
export const TESTS: Record<string, () => void> = {
testCryptoGetRandomValues,
testImplementsBuffer,
testNodeCompatModules,
Expand All @@ -19,6 +19,10 @@ export default {
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
const testName = url.pathname.slice(1);
if (testName === "ping") {
// Used by the deploy test to know when the worker is online
return new Response("pong");
}
const test = TESTS[testName];
if (!test) {
return new Response(
Expand Down Expand Up @@ -157,7 +161,9 @@ async function testTimers() {
const timers = await import("node:timers");
const timeout = timers.setTimeout(() => null, 1000);
// active is deprecated and no more in the type
(timers as any).active(timeout);
(timers as unknown as { active: (t: NodeJS.Timeout) => void }).active(
timeout
);
timers.clearTimeout(timeout);

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

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

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

const exampleLog = debug.default("example");
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"dependencies": {
"@cloudflare/kv-asset-handler": "workspace:*",
"@cloudflare/unenv-preset": "2.3.3",
"@cloudflare/unenv-preset": "workspace:*",
"blake3-wasm": "2.1.5",
"esbuild": "catalog:default",
"miniflare": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"templates",
"**/__tests__",
"**/*.test.ts",
"kv-asset-handler.js"
"kv-asset-handler.js",
"e2e"
]
}
32 changes: 4 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading