Skip to content

Commit 3756ab2

Browse files
add config redirect fixture with tests
1 parent 15dc38b commit 3756ab2

File tree

11 files changed

+141
-0
lines changed

11 files changed

+141
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
async fetch(request, env) {
3+
return new Response("Generated: " + env.generated ?? false);
4+
},
5+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "redirected-config-worker",
3+
"compatibility_date": "2024-12-01",
4+
"main": "index.js",
5+
"vars": {
6+
"generated": true
7+
}
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "redirected-config-worker",
3+
"private": true,
4+
"description": "",
5+
"license": "ISC",
6+
"author": "",
7+
"main": "src/index.js",
8+
"scripts": {
9+
"build": "node -r esbuild-register tools/build.ts",
10+
"check:type": "tsc",
11+
"dev": "pnpm run build && wrangler dev",
12+
"test:ci": "pnpm run build && vitest run"
13+
},
14+
"devDependencies": {
15+
"@cloudflare/workers-tsconfig": "workspace:^",
16+
"undici": "catalog:default",
17+
"wrangler": "workspace:*"
18+
},
19+
"volta": {
20+
"extends": "../../package.json"
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
async fetch(request, env) {
3+
return new Response("Generated: " + env.generated ?? false);
4+
},
5+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { resolve } from "path";
2+
import { fetch } from "undici";
3+
import { describe, it } from "vitest";
4+
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";
5+
6+
const basePath = resolve(__dirname, "..");
7+
8+
describe("'wrangler dev' correctly renders pages", () => {
9+
it("uses the generated config", async ({ expect, onTestFinished }) => {
10+
const { ip, port, stop } = await runWranglerDev(basePath, [
11+
"--port=0",
12+
"--inspector-port=0",
13+
]);
14+
onTestFinished(async () => await stop?.());
15+
16+
// Note that the local protocol defaults to http
17+
const response = await fetch(`http://${ip}:${port}/`);
18+
const text = await response.text();
19+
expect(response.status).toBe(200);
20+
expect(text).toMatchInlineSnapshot(`"Generated: true"`);
21+
});
22+
23+
it("uses a the config from command line rather than generated config", async ({
24+
expect,
25+
onTestFinished,
26+
}) => {
27+
const { ip, port, stop } = await runWranglerDev(basePath, [
28+
"-c=wrangler.toml",
29+
"--port=0",
30+
"--inspector-port=0",
31+
]);
32+
onTestFinished(async () => await stop?.());
33+
34+
// Note that the local protocol defaults to http
35+
const response = await fetch(`http://${ip}:${port}/`);
36+
const text = await response.text();
37+
expect(response.status).toBe(200);
38+
expect(text).toMatchInlineSnapshot(`"Generated: undefined"`);
39+
});
40+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { copyFileSync, mkdirSync, rmSync, writeFileSync } from "fs";
2+
3+
// Create a pseudo build directory
4+
rmSync("build", { recursive: true, force: true });
5+
mkdirSync("build");
6+
const config = {
7+
name: "redirected-config-worker",
8+
compatibility_date: "2024-12-01",
9+
main: "index.js",
10+
vars: { generated: true },
11+
};
12+
writeFileSync("build/wrangler.json", JSON.stringify(config, undefined, 2));
13+
copyFileSync("src/index.js", "build/index.js");
14+
15+
// Create the redirect file
16+
rmSync(".wrangler/deploy", { recursive: true, force: true });
17+
mkdirSync(".wrangler/deploy", { recursive: true });
18+
const redirect = { configPath: "../../build/wrangler.json" };
19+
writeFileSync(
20+
".wrangler/deploy/config.json",
21+
JSON.stringify(redirect, undefined, 2)
22+
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"esModuleInterop": true,
5+
"module": "CommonJS",
6+
"lib": ["ES2020"],
7+
"types": ["node"],
8+
"skipLibCheck": true,
9+
"moduleResolution": "node",
10+
"noEmit": true
11+
},
12+
"include": ["tests", "../../node-types.d.ts"]
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineProject, mergeConfig } from "vitest/config";
2+
import configShared from "../../vitest.shared";
3+
4+
export default mergeConfig(
5+
configShared,
6+
defineProject({
7+
test: {},
8+
})
9+
);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "redirected-config-worker"
2+
compatibility_date = "2024-12-01"
3+
4+
main = "src/index.js"

0 commit comments

Comments
 (0)