Repository navigation
bun test: only run process.on('exit') listeners when node:test APIs were used #38442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
44df25d
8547c50
e8803f1
56f851a
cd40e7b
9b7b428
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -145,6 +145,9 @@ pub struct TestRunner<'a> { | |||||||||||||||||||||||||
| pub(crate) unhandled_errors_between_tests: u32, | ||||||||||||||||||||||||||
| pub(crate) summary: Summary, | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Set once any `node:test` registration API is called; gates `process.on('exit')` dispatch at the end of the run. | ||||||||||||||||||||||||||
| pub(crate) node_test_used: bool, | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| pub(crate) bun_test_root: bun_test::BunTestRoot, | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -513,17 +516,21 @@ pub mod Jest { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Reached only from `node:test`, through `$newRustFunction` rather than the | ||||||||||||||||||||||||||
| /// public `bun:test` module object. Returns 0 outside `bun test`. | ||||||||||||||||||||||||||
| /// public `bun:test` module object, whenever a node:test API registers something. Returns 0 outside `bun test`. | ||||||||||||||||||||||||||
| pub(crate) fn js_file_generation( | ||||||||||||||||||||||||||
| _global: &JSGlobalObject, | ||||||||||||||||||||||||||
| global: &JSGlobalObject, | ||||||||||||||||||||||||||
| _callframe: &CallFrame, | ||||||||||||||||||||||||||
| ) -> JsResult<JSValue> { | ||||||||||||||||||||||||||
| // `runner_ptr()` rather than `runner()`: node:test calls this on every test | ||||||||||||||||||||||||||
| // registration, and an exclusive `&mut TestRunner` would invalidate the | ||||||||||||||||||||||||||
| // `bun_test_root` pointer `test_command.rs` keeps live across the file run. | ||||||||||||||||||||||||||
| // SAFETY: same invariant as `runner()` — RUNNER is only read on the JS thread. | ||||||||||||||||||||||||||
| let generation = | ||||||||||||||||||||||||||
| Jest::runner_ptr().map_or(0, |p| unsafe { (*p.as_ptr()).bun_test_root.file_generation }); | ||||||||||||||||||||||||||
| let generation = Jest::runner_ptr().map_or(0, |p| unsafe { | ||||||||||||||||||||||||||
| if global.bun_vm().worker_ref().is_none() { | ||||||||||||||||||||||||||
| (*p.as_ptr()).node_test_used = true; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| (*p.as_ptr()).bun_test_root.file_generation | ||||||||||||||||||||||||||
|
Comment on lines
+528
to
+532
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Check the Worker state before reading Line 528 reads Proposed fix pub(crate) fn js_file_generation(
global: &JSGlobalObject,
_callframe: &CallFrame,
) -> JsResult<JSValue> {
+ if global.bun_vm().worker_ref().is_some() {
+ return Ok(JSValue::from(0));
+ }
+
// `runner_ptr()` rather than `runner()`: node:test calls this on every test
// registration, and an exclusive `&mut TestRunner` would invalidate the
// `bun_test_root` pointer `test_command.rs` keeps live across the file run.
// SAFETY: same invariant as `runner()` — RUNNER is only read on the JS thread.
let generation = Jest::runner_ptr().map_or(0, |p| unsafe {
- if global.bun_vm().worker_ref().is_none() {
- (*p.as_ptr()).node_test_used = true;
- }
+ (*p.as_ptr()).node_test_used = true;
(*p.as_ptr()).bun_test_root.file_generation
});As per coding guidelines, “Respect thread affinity” and “synchronize shared state.” 📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| Ok(JSValue::from(generation)) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1570,49 +1570,180 @@ describe("bun test", () => { | |||||||||||||||||||||||||||||||
| expect(output).toContain("app message"); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| test("runs process.on('exit') handlers", async () => { | ||||||||||||||||||||||||||||||||
| using dir = tempDir("bun-test-exit-handler", { | ||||||||||||||||||||||||||||||||
| "exit.test.ts": ` | ||||||||||||||||||||||||||||||||
| import { test } from "bun:test"; | ||||||||||||||||||||||||||||||||
| process.on("exit", () => console.log("exit handler ran")); | ||||||||||||||||||||||||||||||||
| test("a test", () => {}); | ||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||
| // jest and vitest never run a test file's process.on('exit') listeners; node's test harness asserts from them. | ||||||||||||||||||||||||||||||||
| describe.concurrent("process.on('exit') listeners", () => { | ||||||||||||||||||||||||||||||||
| async function runFiles(files: Record<string, string>, ...args: string[]) { | ||||||||||||||||||||||||||||||||
| using dir = tempDir("bun-test-exit-listener", files); | ||||||||||||||||||||||||||||||||
| await using proc = Bun.spawn({ | ||||||||||||||||||||||||||||||||
| cmd: [bunExe(), "test", ...args], | ||||||||||||||||||||||||||||||||
| env: bunEnv, | ||||||||||||||||||||||||||||||||
| cwd: String(dir), | ||||||||||||||||||||||||||||||||
| stderr: "pipe", | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||||||||||||||||||||||||||||||||
| return { stdout, stderr, exitCode }; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function runFile(name: string, contents: string) { | ||||||||||||||||||||||||||||||||
| return runFiles({ [name]: contents }, name); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const bunTestFile = (n: number) => ` | ||||||||||||||||||||||||||||||||
| import { test } from "bun:test"; | ||||||||||||||||||||||||||||||||
| process.on("exit", () => { | ||||||||||||||||||||||||||||||||
| console.log("exit listener ${n} ran"); | ||||||||||||||||||||||||||||||||
| process.exit(1); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| test("test ${n}", () => {}); | ||||||||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||||||||
| const nodeTestFile = (n: number) => ` | ||||||||||||||||||||||||||||||||
| import { test } from "node:test"; | ||||||||||||||||||||||||||||||||
| process.on("exit", () => process.exit(1)); | ||||||||||||||||||||||||||||||||
| test("test ${n}", () => {}); | ||||||||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| test("are not run for a bun:test file", async () => { | ||||||||||||||||||||||||||||||||
| const { stdout, stderr, exitCode } = await runFile("exit.test.ts", bunTestFile(1)); | ||||||||||||||||||||||||||||||||
| expect(stdout).not.toContain("exit listener"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("1 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(0); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| test("are not run for a file that registers globals-style tests", async () => { | ||||||||||||||||||||||||||||||||
| const { stdout, stderr, exitCode } = await runFile( | ||||||||||||||||||||||||||||||||
| "globals.test.ts", | ||||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||||
| process.on("exit", () => { | ||||||||||||||||||||||||||||||||
| console.log("exit listener ran"); | ||||||||||||||||||||||||||||||||
| process.exit(1); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| test("a passing test", () => {}); | ||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| expect(stdout).not.toContain("exit listener ran"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("1 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(0); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| await using proc = Bun.spawn({ | ||||||||||||||||||||||||||||||||
| cmd: [bunExe(), "test", "exit.test.ts"], | ||||||||||||||||||||||||||||||||
| env: bunEnv, | ||||||||||||||||||||||||||||||||
| cwd: String(dir), | ||||||||||||||||||||||||||||||||
| stderr: "pipe", | ||||||||||||||||||||||||||||||||
| test("are not run when node:test is only imported", async () => { | ||||||||||||||||||||||||||||||||
| const { stdout, stderr, exitCode } = await runFile( | ||||||||||||||||||||||||||||||||
| "node-import-only.test.ts", | ||||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||||
| import { test } from "bun:test"; | ||||||||||||||||||||||||||||||||
| import { mock } from "node:test"; | ||||||||||||||||||||||||||||||||
| void mock; | ||||||||||||||||||||||||||||||||
| process.on("exit", () => { | ||||||||||||||||||||||||||||||||
| console.log("exit listener ran"); | ||||||||||||||||||||||||||||||||
| process.exit(1); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| test("a passing test", () => {}); | ||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| expect(stdout).not.toContain("exit listener ran"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("1 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(0); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||||||||||||||||||||||||||||||||
| expect(stdout).toContain("exit handler ran"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("1 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(0); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| test("are not run when only a Worker uses node:test", async () => { | ||||||||||||||||||||||||||||||||
| const { stdout, stderr, exitCode } = await runFiles( | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| "worker-uses-node-test.test.ts": ` | ||||||||||||||||||||||||||||||||
| import { test } from "bun:test"; | ||||||||||||||||||||||||||||||||
| process.on("exit", () => { | ||||||||||||||||||||||||||||||||
| console.log("exit listener ran"); | ||||||||||||||||||||||||||||||||
| process.exit(1); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| test("a Worker uses node:test", async () => { | ||||||||||||||||||||||||||||||||
| const worker = new Worker(new URL("./worker.ts", import.meta.url)); | ||||||||||||||||||||||||||||||||
| await new Promise(resolve => worker.addEventListener("message", resolve, { once: true })); | ||||||||||||||||||||||||||||||||
| worker.terminate(); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+1656
to
+1662
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Settle and terminate the Worker on every path. If the Worker emits Proposed fix test("a Worker uses node:test", async () => {
const worker = new Worker(new URL("./worker.ts", import.meta.url));
- await new Promise(resolve => worker.addEventListener("message", resolve, { once: true }));
- worker.terminate();
+ try {
+ await new Promise<void>((resolve, reject) => {
+ worker.addEventListener("message", () => resolve(), { once: true });
+ worker.addEventListener("error", reject, { once: true });
+ });
+ } finally {
+ await worker.terminate();
+ }
});As per coding guidelines, tests must wire failure events to rejection and release resources on all paths. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines
claude[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||
| "worker.ts": ` | ||||||||||||||||||||||||||||||||
| import { mock } from "node:test"; | ||||||||||||||||||||||||||||||||
| mock.fn(() => {}); | ||||||||||||||||||||||||||||||||
| postMessage("used"); | ||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| "worker-uses-node-test.test.ts", | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| expect(stdout).not.toContain("exit listener ran"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("1 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(0); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| test("an exit handler can fail the run, like node's common.mustCall()", async () => { | ||||||||||||||||||||||||||||||||
| using dir = tempDir("bun-test-exit-handler-code", { | ||||||||||||||||||||||||||||||||
| "exit-code.test.ts": ` | ||||||||||||||||||||||||||||||||
| import { test } from "bun:test"; | ||||||||||||||||||||||||||||||||
| process.on("exit", () => process.exit(1)); | ||||||||||||||||||||||||||||||||
| test("a passing test", () => {}); | ||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||
| test("still run when the file itself calls process.exit()", async () => { | ||||||||||||||||||||||||||||||||
| const { stdout, exitCode } = await runFile( | ||||||||||||||||||||||||||||||||
| "explicit-exit.test.ts", | ||||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||||
| import { test } from "bun:test"; | ||||||||||||||||||||||||||||||||
| process.on("exit", code => console.log("exit listener ran with", code)); | ||||||||||||||||||||||||||||||||
| test("exits", () => process.exit(3)); | ||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| expect(stdout).toContain("exit listener ran with 3"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(3); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| await using proc = Bun.spawn({ | ||||||||||||||||||||||||||||||||
| cmd: [bunExe(), "test", "exit-code.test.ts"], | ||||||||||||||||||||||||||||||||
| env: bunEnv, | ||||||||||||||||||||||||||||||||
| cwd: String(dir), | ||||||||||||||||||||||||||||||||
| stderr: "pipe", | ||||||||||||||||||||||||||||||||
| test("run once a node:test API registers a test", async () => { | ||||||||||||||||||||||||||||||||
| const { stdout, stderr, exitCode } = await runFile( | ||||||||||||||||||||||||||||||||
| "node.test.ts", | ||||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||||
| import { test } from "node:test"; | ||||||||||||||||||||||||||||||||
| process.on("exit", code => console.log("exit listener ran with", code)); | ||||||||||||||||||||||||||||||||
| test("a passing test", () => {}); | ||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| expect(stdout).toContain("exit listener ran with 0"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("1 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(0); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||||||||||||||||||||||||||||||||
| // Windows prints the banner to stdout; only assert nothing test-shaped leaks. | ||||||||||||||||||||||||||||||||
| expect(stdout).not.toContain("pass"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("1 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(1); | ||||||||||||||||||||||||||||||||
| test("can fail the run once node:test registered a test, like node's common.mustCall()", async () => { | ||||||||||||||||||||||||||||||||
| const { stderr, exitCode } = await runFile("node-exit-code.test.ts", nodeTestFile(1)); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("1 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(1); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| test("run for a bun:test file under BUN_TEST_DRAIN_EVENT_LOOP, which the vendored node tests set", async () => { | ||||||||||||||||||||||||||||||||
| using dir = tempDir("bun-test-exit-listener", { "drain.test.ts": bunTestFile(1) }); | ||||||||||||||||||||||||||||||||
| await using proc = Bun.spawn({ | ||||||||||||||||||||||||||||||||
| cmd: [bunExe(), "test", "drain.test.ts"], | ||||||||||||||||||||||||||||||||
| env: { ...bunEnv, BUN_TEST_DRAIN_EVENT_LOOP: "1" }, | ||||||||||||||||||||||||||||||||
| cwd: String(dir), | ||||||||||||||||||||||||||||||||
| stderr: "pipe", | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||||||||||||||||||||||||||||||||
| expect(stdout).toContain("exit listener 1 ran"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("1 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(1); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| test("are not run in --parallel workers for bun:test files", async () => { | ||||||||||||||||||||||||||||||||
| const { stdout, stderr, exitCode } = await runFiles( | ||||||||||||||||||||||||||||||||
| { "a.test.ts": bunTestFile(1), "b.test.ts": bunTestFile(2) }, | ||||||||||||||||||||||||||||||||
| "--parallel=2", | ||||||||||||||||||||||||||||||||
| "a.test.ts", | ||||||||||||||||||||||||||||||||
| "b.test.ts", | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| // Worker output is relayed on the coordinator's stderr. | ||||||||||||||||||||||||||||||||
| expect(stdout + stderr).not.toContain("exit listener"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("2 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(0); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // --parallel implies --isolate, and a file's listeners are torn down with its global before the worker exits. | ||||||||||||||||||||||||||||||||
| test("--parallel workers exit cleanly after node:test files; listeners do not reach the run's exit code", async () => { | ||||||||||||||||||||||||||||||||
| const { stderr, exitCode } = await runFiles( | ||||||||||||||||||||||||||||||||
| { "a.test.ts": nodeTestFile(1), "b.test.ts": nodeTestFile(2) }, | ||||||||||||||||||||||||||||||||
| "--parallel=2", | ||||||||||||||||||||||||||||||||
| "a.test.ts", | ||||||||||||||||||||||||||||||||
| "b.test.ts", | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| expect(stderr).not.toContain("worker crashed"); | ||||||||||||||||||||||||||||||||
| expect(stderr).toContain("2 pass"); | ||||||||||||||||||||||||||||||||
| expect(exitCode).toBe(0); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Document the event-loop-drain override.
BUN_TEST_DRAIN_EVENT_LOOP=1also clearsskip_exit_listenerswithoutnode:testuse. Update this comment so it matchesskip_exit_listeners().As per coding guidelines, comments should contain only durable non-obvious information.
🤖 Prompt for AI Agents
Source: Coding guidelines