-
Notifications
You must be signed in to change notification settings - Fork 5k
bun:test: defer the AsyncContextFrame wrap past .length/.bind() for tests and hooks #35334
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test"; | ||
| import { AsyncLocalStorage } from "node:async_hooks"; | ||
|
|
||
| // When a test or hook is registered while an AsyncLocalStorage context is | ||
| // active, bun:test wraps the callback in an AsyncContextFrame so the context | ||
| // is restored at call time. The wrapper has no `.length` and is not itself | ||
| // callable via JSC::getCallData, so `.length` (done-param detection) and | ||
| // `.bind()` (test.each) must be read from the user's function before wrapping. | ||
|
|
||
| const als = new AsyncLocalStorage<{ tag: string }>(); | ||
| const order: string[] = []; | ||
|
|
||
| describe("registered inside an active ALS context", () => { | ||
| als.run({ tag: "collection" }, () => { | ||
| beforeAll(function zeroArg() { | ||
| order.push("beforeAll"); | ||
| }); | ||
| beforeEach(function zeroArg() { | ||
| order.push("beforeEach"); | ||
| }); | ||
| afterEach(function zeroArg() { | ||
| order.push("afterEach"); | ||
| }); | ||
| afterAll(function zeroArg() { | ||
| order.push("afterAll"); | ||
| }); | ||
| afterAll(function withDone(done) { | ||
| order.push("afterAll-done"); | ||
| setImmediate(done); | ||
| }); | ||
|
|
||
| test("zero-arg test", function zeroArg() { | ||
| order.push("zero-arg test"); | ||
| expect(als.getStore()?.tag).toBe("collection"); | ||
| }); | ||
|
|
||
| test("one-arg test still receives done", function withDone(done) { | ||
| order.push("done test"); | ||
| expect(typeof done).toBe("function"); | ||
| expect(als.getStore()?.tag).toBe("collection"); | ||
| setImmediate(done); | ||
| }); | ||
|
|
||
| test.each([[1], [2]])("each %p", function withArg(n) { | ||
| order.push(`each ${n}`); | ||
| expect(als.getStore()?.tag).toBe("collection"); | ||
| }); | ||
|
|
||
| describe("nested describe", function zeroArg() { | ||
| afterAll(function zeroArg() { | ||
| order.push("nested.afterAll"); | ||
| }); | ||
| test("passes", function zeroArg() { | ||
| order.push("nested.test"); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| test("hooks and tests registered inside an ALS context use the callback's real arity", () => { | ||
| expect(order).toEqual([ | ||
| "beforeAll", | ||
| "beforeEach", | ||
| "zero-arg test", | ||
| "afterEach", | ||
| "beforeEach", | ||
| "done test", | ||
| "afterEach", | ||
| "beforeEach", | ||
| "each 1", | ||
| "afterEach", | ||
| "beforeEach", | ||
| "each 2", | ||
| "afterEach", | ||
| "beforeEach", | ||
| "nested.test", | ||
| "afterEach", | ||
| "nested.afterAll", | ||
| "afterAll", | ||
| "afterAll-done", | ||
| ]); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, normalizeBunSnapshot } from "harness"; | ||
| import path from "node:path"; | ||
|
|
||
| // Registering a zero-arg test() or hook inside an active AsyncLocalStorage | ||
| // context used to be treated as taking a done callback (the AsyncContextFrame | ||
| // wrapper has no `.length`), so the callback would wait for done() and time | ||
| // out; test.each() threw "bind() called on non-callable" on the same wrapper. | ||
| // Run the fixture with a short per-test timeout so the unfixed build fails | ||
| // fast rather than sitting on the 5s default for each entry. | ||
| test("tests and hooks registered inside an AsyncLocalStorage context detect done-callback arity correctly", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "test", "--timeout=500", path.join(import.meta.dir, "als-hook-arity.fixture.ts")], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(normalizeBunSnapshot(stderr)).toMatchInlineSnapshot(` | ||
| "test/js/bun/test/als-hook-arity.fixture.ts: | ||
| (pass) registered inside an active ALS context > zero-arg test | ||
| (pass) registered inside an active ALS context > one-arg test still receives done | ||
| (pass) registered inside an active ALS context > each 1 | ||
| (pass) registered inside an active ALS context > each 2 | ||
| (pass) registered inside an active ALS context > nested describe > passes | ||
| (pass) hooks and tests registered inside an ALS context use the callback's real arity | ||
|
|
||
| 6 pass | ||
| 0 fail | ||
| 6 expect() calls | ||
| Ran 6 tests across 1 file." | ||
| `); | ||
| expect(stdout).toStartWith("bun test "); | ||
| expect(exitCode).toBe(0); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.