Skip to content

Commit

Permalink
fix: cleanup after pages dev tests
Browse files Browse the repository at this point in the history
We weren't killing the process started by wrangler whenever its parent was killed. This fix is to listen on SIGTERM/SIGTERM and kill that process. This also lets us remove `--forceExit` from the test runner calls. I also did some minor configuration cleanups.
  • Loading branch information
threepointone committed Mar 23, 2022
1 parent 990316f commit 2f70d9c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .changeset/young-boats-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"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 SIGTERM/SIGTERM and kill that process. This also lets us remove `--forceExit` from the test runner calls. I also did some minor configuration cleanups.
8 changes: 4 additions & 4 deletions packages/example-pages-functions-app/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"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"
"test": "npx jest"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.2.0",
Expand All @@ -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"
}
}
5 changes: 3 additions & 2 deletions 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 All @@ -10,7 +11,7 @@
"dev:remix": "remix watch",
"dev:wrangler": "wrangler pages dev ./public",
"start": "npm run dev:wrangler",
"test": "npx jest --forceExit"
"test": "npx jest"
},
"dependencies": {
"@remix-run/cloudflare-pages": "^1.1.3",
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();

0 comments on commit 2f70d9c

Please sign in to comment.