Skip to content

Commit

Permalink
fix(node): don't emit exit twice (#1753)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored Dec 23, 2021
1 parent 956c36a commit 09802f3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
8 changes: 4 additions & 4 deletions node/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ class Process extends EventEmitter {
constructor() {
super();

//This causes the exit event to be binded to the unload event
globalThis.addEventListener("unload", () => {
//TODO(Soremwar)
//Get the exit code from the unload event
super.emit("exit", 0);
if (!process._exiting) {
process._exiting = true;
super.emit("exit", process.exitCode || 0);
}
});
}

Expand Down
27 changes: 27 additions & 0 deletions node/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,30 @@ Deno.test("process._exiting", () => {
Deno.test("process.execPath", () => {
assertEquals(process.execPath, process.argv[0]);
});

Deno.test({
name: "process.exit",
async fn() {
const cwd = path.dirname(path.fromFileUrl(import.meta.url));

const p = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--quiet",
"--unstable",
"./testdata/process_exit2.ts",
],
cwd,
stdout: "piped",
});

const decoder = new TextDecoder();
const rawOutput = await p.output();
assertEquals(
stripColor(decoder.decode(rawOutput).trim()),
"exit",
);
p.close();
},
});
5 changes: 5 additions & 0 deletions node/testdata/process_exit2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "../global.ts";

//deno-lint-ignore no-undef
process.on("exit", () => console.log("exit"));
process.exit();

0 comments on commit 09802f3

Please sign in to comment.