Skip to content

Commit

Permalink
fix: cleanup after pages dev tests (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone authored Mar 23, 2022
1 parent 990316f commit 82e4143
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 17 deletions.
10 changes: 10 additions & 0 deletions .changeset/young-boats-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"wrangler": patch
---

fix: cleanup after `pages dev` tests

We weren't killing the process started by wrangler whenever its parent was killed. This fix is to listen on SIGINT/SIGTERM and kill that process. I also did some minor configuration cleanups.

Fixes https://github.com/cloudflare/wrangler2/issues/397
Fixes https://github.com/cloudflare/wrangler2/issues/618
6 changes: 3 additions & 3 deletions packages/example-pages-functions-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"name": "example-pages-functions-app",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "npx wrangler pages dev public --port 8789",
"test": "npx jest --forceExit"
Expand Down Expand Up @@ -29,6 +30,5 @@
}
]
}
},
"version": null
}
}
17 changes: 13 additions & 4 deletions packages/example-pages-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn } from "child_process";
import { resolve } from "path";
import * as path from "path";
import { fetch } from "undici";
import type { ChildProcess } from "child_process";
import type { Response } from "undici";
Expand All @@ -26,7 +26,7 @@ describe("Pages Functions", () => {
beforeAll(async () => {
wranglerProcess = spawn("npm", ["run", "dev"], {
shell: isWindows,
cwd: resolve(__dirname, "../"),
cwd: path.resolve(__dirname, "../"),
env: { BROWSER: "none", ...process.env },
});
wranglerProcess.stdout.on("data", (chunk) => {
Expand All @@ -37,8 +37,17 @@ describe("Pages Functions", () => {
});
});

afterAll(() => {
wranglerProcess.kill();
afterAll(async () => {
await new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
wranglerProcess.kill();
});
});

it("renders static pages", async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/example-pages-functions-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target": "ES2020",
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["@cloudflare/workers-types"]
"types": ["@cloudflare/workers-types"],
"moduleResolution": "node"
}
}
3 changes: 2 additions & 1 deletion packages/example-remix-pages-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"name": "example-remix-pages-app",
"version": "0.0.0",
"private": true,
"description": "",
"license": "",
"scripts": {
Expand Down
23 changes: 16 additions & 7 deletions packages/example-remix-pages-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn, spawnSync } from "child_process";
import { resolve } from "path";
import * as path from "path";
import { fetch } from "undici";
import type { ChildProcess } from "child_process";
import type { Response } from "undici";
Expand All @@ -26,23 +26,32 @@ describe("Remix", () => {
beforeAll(async () => {
spawnSync("npm", ["run", "build"], {
shell: isWindows,
cwd: resolve(__dirname, "../"),
cwd: path.resolve(__dirname, "../"),
});
wranglerProcess = spawn("npm", ["run", "dev:wrangler"], {
shell: isWindows,
cwd: resolve(__dirname, "../"),
cwd: path.resolve(__dirname, "../"),
env: { BROWSER: "none", ...process.env },
});
wranglerProcess.stdout.on("data", (chunk) => {
wranglerProcess.stdout?.on("data", (chunk) => {
console.log(chunk.toString());
});
wranglerProcess.stderr.on("data", (chunk) => {
wranglerProcess.stderr?.on("data", (chunk) => {
console.log(chunk.toString());
});
});

afterAll(() => {
wranglerProcess.kill();
afterAll(async () => {
await new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
wranglerProcess.kill();
});
});

it("renders", async () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/wrangler/bin/wrangler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const semiver = require("semiver");

const MIN_NODE_VERSION = "16.7.0";

let wranglerProcess;

async function main() {
if (semiver(process.versions.node, MIN_NODE_VERSION) < 0) {
// Note Volta and nvm are also recommended in the official docs:
Expand All @@ -18,7 +20,7 @@ Consider using a Node.js version manager such as https://volta.sh/ or https://gi
return;
}

spawn(
wranglerProcess = spawn(
process.execPath,
[
"--no-warnings",
Expand All @@ -33,4 +35,11 @@ Consider using a Node.js version manager such as https://volta.sh/ or https://gi
);
}

process.on("SIGINT", () => {
wranglerProcess.kill();
});
process.on("SIGTERM", () => {
wranglerProcess.kill();
});

void main();
1 change: 1 addition & 0 deletions packages/wrangler/src/pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const EXIT = (message?: string, code?: number) => {
if (message) console.log(message);
if (code) process.exitCode = code;
EXIT_CALLBACKS.forEach((callback) => callback());
RUNNING_BUILDERS.forEach((builder) => builder.stop?.());
process.exit(code);
};

Expand Down

0 comments on commit 82e4143

Please sign in to comment.