Skip to content

Commit a56d5a7

Browse files
committed
Move the unenv tests from the preset to wrangler e2e
1 parent a355327 commit a56d5a7

File tree

15 files changed

+184
-86
lines changed

15 files changed

+184
-86
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",

packages/wrangler/e2e/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
"jsx": "preserve" // not used, but required for import of types (which transitively import jsx)
77
},
88
"include": ["vitest.config.mts", "**/*.ts", "../src/*.d.ts"],
9-
"exclude": []
9+
"exclude": ["unenv-preset"]
1010
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import path from "node:path";
2+
import { fileURLToPath } from "node:url";
3+
4+
const thisPath = path.dirname(fileURLToPath(import.meta.url));
5+
6+
/** Path to the worker script */
7+
export const workerScript = path.join(thisPath, "/worker/index.ts");
8+
9+
/**
10+
* @returns The current date formatted as YYYY-MM-DD
11+
*/
12+
export function getYMDDate(): string {
13+
const date = new Date();
14+
const year = date.getFullYear();
15+
// Months are 0-indexed
16+
const month = (date.getMonth() + 1).toString().padStart(2, "0");
17+
const day = date.getDate().toString().padStart(2, "0");
18+
19+
return `${year}-${month}-${day}`;
20+
}
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 dedent from "ts-dedent";
6+
import { fetch } from "undici";
7+
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
8+
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
9+
import { generateResourceName } from "../helpers/generate-resource-name";
10+
import { getYMDDate, workerScript } from "./helper";
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": dedent`{
22+
"name": "${generateResourceName()}",
23+
"main": "${workerScript}",
24+
"compatibility_date": "${getYMDDate()}",
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 dedent from "ts-dedent";
8+
import { fetch } from "undici";
9+
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
10+
import { CLOUDFLARE_ACCOUNT_ID } from "../helpers/account-id";
11+
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
12+
import { generateResourceName } from "../helpers/generate-resource-name";
13+
import { retry } from "../helpers/retry";
14+
import { getYMDDate, workerScript } from "./helper";
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": dedent`{
27+
"name": "${generateResourceName()}",
28+
"main": "${workerScript}",
29+
"compatibility_date": "${getYMDDate()}",
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+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["node"],
5+
"module": "nodenext",
6+
"moduleResolution": "nodenext"
7+
},
8+
"include": ["**/*.ts"]
9+
}

0 commit comments

Comments
 (0)