-
Notifications
You must be signed in to change notification settings - Fork 5k
bun:test: bound expect() promise waits by the test timeout #36716
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
Open
robobun
wants to merge
6
commits into
main
Choose a base branch
from
claude/farm/3130629c/expect-resolves-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−5
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
90b3201
bun:test: bound expect() promise waits by the test timeout
robobun d44eed9
[autofix.ci] apply automated fixes
autofix-ci[bot] 6b7fd11
trim doc comments on wait_for_promise_bounded_by_test
robobun ad31381
collapse doc comment to one line
robobun c14e9e3
mark toThrow async-fn promise handled before the bounded wait
robobun 0ebfa04
ci: retrigger
robobun 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // https://github.com/oven-sh/bun/issues/14950 | ||
| // `expect(pendingPromise).resolves.<matcher>()` in a sync test body must not | ||
| // hang `bun test` forever at 100% CPU; the per-test timeout has to fire. | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| async function runTestFile(name: string, body: string) { | ||
| using dir = tempDir(name, { "t.test.js": body }); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "test", "t.test.js", "--timeout", "500"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stderr: "pipe", | ||
| stdout: "pipe", | ||
| }); | ||
| // On an unfixed build the inner runner never exits (the per-test --timeout | ||
| // cannot interrupt the wait_for_promise spin), so kill it ourselves instead | ||
| // of letting the outer runner's own timeout abort the assertion. | ||
| let hung = false; | ||
| const watchdog = setTimeout(() => { | ||
| hung = true; | ||
| proc.kill(); | ||
| }, 20_000); | ||
| try { | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout, stderr, exitCode, hung }; | ||
| } finally { | ||
| clearTimeout(watchdog); | ||
| } | ||
| } | ||
|
|
||
| describe.concurrent("expect().resolves/.rejects on a not-yet-settled promise", () => { | ||
| test(".resolves on a promise resolved after the matcher call times out instead of hanging", async () => { | ||
| const { stderr, exitCode, hung } = await runTestFile( | ||
| "issue-14950-resolves", | ||
| `test("promise resolves after expect call", () => { | ||
| let resolve; | ||
| expect(new Promise(r => (resolve = r))).resolves.toBe(25); | ||
| resolve(25); | ||
| });`, | ||
| ); | ||
| expect(hung).toBe(false); | ||
| expect(stderr).toContain("still pending"); | ||
| expect(stderr).toMatch(/timed out after \d+ms/); | ||
| expect(exitCode).toBe(1); | ||
| }, 60_000); | ||
|
|
||
| test(".rejects on a never-settling promise times out instead of hanging", async () => { | ||
| const { stderr, exitCode, hung } = await runTestFile( | ||
| "issue-14950-rejects", | ||
| `test("never settles", () => { | ||
| expect(new Promise(() => {})).rejects.toThrow(); | ||
| });`, | ||
| ); | ||
| expect(hung).toBe(false); | ||
| expect(stderr).toContain("still pending"); | ||
| expect(stderr).toMatch(/timed out after \d+ms/); | ||
| expect(exitCode).toBe(1); | ||
| }, 60_000); | ||
|
|
||
| test("toThrow on an async fn returning a never-settling promise times out instead of hanging", async () => { | ||
| const { stderr, exitCode, hung } = await runTestFile( | ||
| "issue-14950-tothrow", | ||
| `test("never settles", () => { | ||
| expect(() => new Promise(() => {})).toThrow(); | ||
| });`, | ||
| ); | ||
| expect(hung).toBe(false); | ||
| expect(stderr).toContain("did not settle within the test timeout"); | ||
| expect(exitCode).toBe(1); | ||
| }, 60_000); | ||
|
|
||
| test("async custom matcher returning a never-settling promise times out instead of hanging", async () => { | ||
| const { stderr, exitCode, hung } = await runTestFile( | ||
| "issue-14950-custom", | ||
| `const { expect, test } = require("bun:test"); | ||
| expect.extend({ | ||
| toNeverSettle() { return new Promise(() => {}); }, | ||
| }); | ||
| test("never settles", () => { | ||
| expect(1).toNeverSettle(); | ||
| });`, | ||
| ); | ||
| expect(hung).toBe(false); | ||
| expect(stderr).toContain("did not settle within the test timeout"); | ||
| expect(exitCode).toBe(1); | ||
| }, 60_000); | ||
|
|
||
| test("a test after one that spins on .resolves still runs", async () => { | ||
| const { stderr, exitCode, hung } = await runTestFile( | ||
| "issue-14950-next-test", | ||
| `test("hangs", () => { | ||
| expect(new Promise(() => {})).resolves.toBe(1); | ||
| }); | ||
| test("runs after", () => { | ||
| expect(1).toBe(1); | ||
| });`, | ||
| ); | ||
| expect(hung).toBe(false); | ||
| expect(stderr).toMatch(/1 pass/); | ||
| expect(stderr).toMatch(/1 fail/); | ||
| expect(exitCode).toBe(1); | ||
| }, 60_000); | ||
|
|
||
| test("a late rejection from an abandoned toThrow async fn does not fail the next test", async () => { | ||
| const { stderr, exitCode, hung } = await runTestFile( | ||
| "issue-14950-late-reject", | ||
| `test("a", () => { | ||
| expect(async () => { await Bun.sleep(700); throw new Error("late"); }).toThrow(); | ||
| }); | ||
| test("b", async () => { | ||
| await Bun.sleep(900); | ||
| expect(1).toBe(1); | ||
| }, 5000);`, | ||
| ); | ||
| expect(hung).toBe(false); | ||
| expect(stderr).toMatch(/1 pass/); | ||
| expect(stderr).toMatch(/1 fail/); | ||
| expect(stderr).not.toContain("Unhandled"); | ||
| expect(exitCode).toBe(1); | ||
| }, 60_000); | ||
|
|
||
| test(".resolves on an already-resolved promise still passes synchronously", async () => { | ||
| const { stderr, exitCode, hung } = await runTestFile( | ||
| "issue-14950-settled", | ||
| `test("already settled", () => { | ||
| expect(Promise.resolve(25)).resolves.toBe(25); | ||
| });`, | ||
| ); | ||
| expect(hung).toBe(false); | ||
| expect(stderr).toMatch(/1 pass/); | ||
| expect(stderr).not.toContain("still pending"); | ||
| expect(exitCode).toBe(0); | ||
| }, 60_000); | ||
| }); |
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
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.