Skip to content

Commit

Permalink
add config redirect fixture with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Dec 12, 2024
1 parent 15dc38b commit 3756ab2
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions fixtures/redirected-config-worker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
5 changes: 5 additions & 0 deletions fixtures/redirected-config-worker/build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
async fetch(request, env) {
return new Response("Generated: " + env.generated ?? false);
},
};
8 changes: 8 additions & 0 deletions fixtures/redirected-config-worker/build/wrangler.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "redirected-config-worker",
"compatibility_date": "2024-12-01",
"main": "index.js",
"vars": {
"generated": true
}
}
22 changes: 22 additions & 0 deletions fixtures/redirected-config-worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "redirected-config-worker",
"private": true,
"description": "",
"license": "ISC",
"author": "",
"main": "src/index.js",
"scripts": {
"build": "node -r esbuild-register tools/build.ts",
"check:type": "tsc",
"dev": "pnpm run build && wrangler dev",
"test:ci": "pnpm run build && vitest run"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:^",
"undici": "catalog:default",
"wrangler": "workspace:*"
},
"volta": {
"extends": "../../package.json"
}
}
5 changes: 5 additions & 0 deletions fixtures/redirected-config-worker/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
async fetch(request, env) {
return new Response("Generated: " + env.generated ?? false);
},
};
40 changes: 40 additions & 0 deletions fixtures/redirected-config-worker/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { resolve } from "path";
import { fetch } from "undici";
import { describe, it } from "vitest";
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";

const basePath = resolve(__dirname, "..");

describe("'wrangler dev' correctly renders pages", () => {
it("uses the generated config", async ({ expect, onTestFinished }) => {
const { ip, port, stop } = await runWranglerDev(basePath, [
"--port=0",
"--inspector-port=0",
]);
onTestFinished(async () => await stop?.());

// Note that the local protocol defaults to http
const response = await fetch(`http://${ip}:${port}/`);
const text = await response.text();
expect(response.status).toBe(200);
expect(text).toMatchInlineSnapshot(`"Generated: true"`);
});

it("uses a the config from command line rather than generated config", async ({
expect,
onTestFinished,
}) => {
const { ip, port, stop } = await runWranglerDev(basePath, [
"-c=wrangler.toml",
"--port=0",
"--inspector-port=0",
]);
onTestFinished(async () => await stop?.());

// Note that the local protocol defaults to http
const response = await fetch(`http://${ip}:${port}/`);
const text = await response.text();
expect(response.status).toBe(200);
expect(text).toMatchInlineSnapshot(`"Generated: undefined"`);
});
});
22 changes: 22 additions & 0 deletions fixtures/redirected-config-worker/tools/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { copyFileSync, mkdirSync, rmSync, writeFileSync } from "fs";

// Create a pseudo build directory
rmSync("build", { recursive: true, force: true });
mkdirSync("build");
const config = {
name: "redirected-config-worker",
compatibility_date: "2024-12-01",
main: "index.js",
vars: { generated: true },
};
writeFileSync("build/wrangler.json", JSON.stringify(config, undefined, 2));
copyFileSync("src/index.js", "build/index.js");

// Create the redirect file
rmSync(".wrangler/deploy", { recursive: true, force: true });
mkdirSync(".wrangler/deploy", { recursive: true });
const redirect = { configPath: "../../build/wrangler.json" };
writeFileSync(
".wrangler/deploy/config.json",
JSON.stringify(redirect, undefined, 2)
);
13 changes: 13 additions & 0 deletions fixtures/redirected-config-worker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2020",
"esModuleInterop": true,
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["node"],
"skipLibCheck": true,
"moduleResolution": "node",
"noEmit": true
},
"include": ["tests", "../../node-types.d.ts"]
}
9 changes: 9 additions & 0 deletions fixtures/redirected-config-worker/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineProject, mergeConfig } from "vitest/config";
import configShared from "../../vitest.shared";

export default mergeConfig(
configShared,
defineProject({
test: {},
})
);
4 changes: 4 additions & 0 deletions fixtures/redirected-config-worker/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "redirected-config-worker"
compatibility_date = "2024-12-01"

main = "src/index.js"
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 3756ab2

Please sign in to comment.