Skip to content

Commit

Permalink
add pages-dev-proxy-with-script fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Jan 7, 2024
1 parent fcde4fc commit cc410e7
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fixtures/pages-dev-proxy-with-script/_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
fetch() {
return new Response("hello from _worker.js");
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
fetch() {
return new Response("hello from custom/script/path/index.js");
},
};
21 changes: 21 additions & 0 deletions fixtures/pages-dev-proxy-with-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "pages-dev-proxy-with-script",
"version": "0.0.0",
"private": true,
"sideEffects": false,
"scripts": {
"dev": "npx wrangler pages dev public",
"test": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc -p ./tests/tsconfig.json"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "^4.20221111.1",
"undici": "^5.9.1",
"wrangler": "workspace:*"
},
"engines": {
"node": ">=16.13"
}
}
78 changes: 78 additions & 0 deletions fixtures/pages-dev-proxy-with-script/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import path from "node:path";
import { Response, fetch } from "undici";
import { describe, it, expect } from "vitest";
import { ChildProcess, fork } from "node:child_process";
import { setTimeout } from "node:timers/promises";

describe("Pages dev with proxy and a script file", () => {
it("should handle requests using a script from the default _worker.js path", async () => {
const { port, childProcess } = await startWranglerPagesDevProxy();
const combinedResponse = await waitUntilReady(`http://127.0.0.1:${port}/`);
const respText = await combinedResponse.text();
expect(respText).toMatchInlineSnapshot('"hello from _worker.js"');
await terminateChildProcess(childProcess);
});

it("should handle requests using a script from a custom script path", async () => {
const { port, childProcess } = await startWranglerPagesDevProxy([
"--script-path=custom/script/path/index.js",
]);
const combinedResponse = await waitUntilReady(`http://127.0.0.1:${port}/`);
const respText = await combinedResponse.text();
expect(respText).toMatchInlineSnapshot(
'"hello from custom/script/path/index.js"'
);
await terminateChildProcess(childProcess);
});
});

async function startWranglerPagesDevProxy(extraArgs: string[] = []): Promise<{
childProcess: ChildProcess;
port: string;
}> {
return new Promise(async (resolve) => {
const childProcess = fork(
path.join("..", "..", "packages", "wrangler", "bin", "wrangler.js"),
["pages", "dev", "--port=0", "--proxy=9999", ...extraArgs],
{
cwd: path.resolve(__dirname, ".."),
env: { BROWSER: "none", ...process.env },
stdio: ["ignore", "ignore", "ignore", "ipc"],
}
).on("message", (message) => {
const parsedMessage = JSON.parse(message.toString());
resolve({
childProcess,
port: parsedMessage.port,
});
});
debugger;
});
}

function terminateChildProcess(childProcess: ChildProcess): Promise<unknown> {
return new Promise((resolve, reject) => {
childProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
childProcess.kill("SIGTERM");
});
}

async function waitUntilReady(url: string): Promise<Response> {
let response: Response | undefined = undefined;

while (response === undefined) {
await setTimeout(500);

try {
response = await fetch(url);
} catch (e) {}
}

return response as Response;
}
7 changes: 7 additions & 0 deletions fixtures/pages-dev-proxy-with-script/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
"compilerOptions": {
"types": ["node"]
},
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
12 changes: 12 additions & 0 deletions fixtures/pages-dev-proxy-with-script/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"include": ["index.d.ts"],
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["@cloudflare/workers-types"],
"moduleResolution": "node",
"noEmit": true,
"skipLibCheck": true
}
}
10 changes: 10 additions & 0 deletions fixtures/pages-dev-proxy-with-script/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
testTimeout: 10_000,
hookTimeout: 10_000,
teardownTimeout: 10_000,
useAtomics: true,
},
});
15 changes: 15 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 cc410e7

Please sign in to comment.