Skip to content

bun:test: make toBeCloseTo reject -Infinity vs Infinity and apply .not to infinite pairs - #46674

Closed
robobun wants to merge 2 commits into
mainfrom
robobun/ca042a73/tobecloseto-infinity
Closed

robobun wants to merge 2 commits into
mainfrom
robobun/ca042a73/tobecloseto-infinity

Conversation

@robobun

@robobun robobun commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • expect(-Infinity).toBeCloseTo(Infinity) and expect(Infinity).not.toBeCloseTo(Infinity) pass in bun test. Jest 30.5.2 and Vitest 4.1.9 fail both, also with the signs swapped.
  • Cause: src/runtime/test_runner/expect/toBeCloseTo.rs:47-57 changed each -Infinity operand to Infinity, then returned a pass for an infinite pair before it read .not.
  • A differential run against Jest found it. No user reported it.

Fix

  • Two infinities are close only when they are equal, and that pair now goes through the normal .not inversion. No existing failure message changes.
  • Correct because this is Jest's rule (Enhancement/expect-to-be-close-to-with-infinity jestjs/jest#7444), which Bun's expect.closeTo already uses (bindings.cpp:600-604).
  • Verified: new toBeCloseTo() block in test/js/bun/test/expect.test.js. 10 of 29 tests fail on main, 0 with the fix. Also expect-label, expect-assertions, expect-extend.
  • Self-reviewed: 12 concerns raised, 11 addressed. Not done: a merge-decision request on bun test: Vitest compatibility #44833 (no dependency) and a note on toBeInstanceOf (another matcher).

Background

Downsides

  • A test that asserts the old verdict now fails. Known case: blackboardsh/cottontail, tests/js/bun-test-expect-conformance.test.ts:19.
  • Release builds, main -> PR: a finite passing call runs 845 -> 843 instructions. A passing expect(Infinity).toBeCloseTo(Infinity) runs 699 -> 845 (one more pow). Text grows 768 bytes.
Notes

Verdict against Jest. A grid of 14 values x 14 values x 9 precisions x 2 polarities, plus 36 .resolves / .rejects rows (3564 rows, all with number operands and number precisions), run on release builds and on Jest 30.5.2 (node v26.3.0):

  • toBeCloseTo rows unlike Jest 30.5.2: 44 before -> 0 after, of 3564 rows.
  • main -> PR: 44 rows change, all from pass to fail, all in the four cells above (9 precisions plus the .resolves and .rejects forms for each cell).

Cost, measured. Release builds (bun run build:release) of main 5c6d4e2 and of this PR. A gdb 16.3 script single-steps one warm call of the host function Bun__Bindgen__ExpectPrototype__toBeCloseTo and counts instructions. Three runs per binary, all counts identical. perf, valgrind and bloaty are not installed in the container.

  • Finite passing call expect(1.001).toBeCloseTo(1), instructions inside the host function: 103 (main) -> 101 (PR). Inclusive: 845 -> 843.
  • expect(Infinity).toBeCloseTo(Infinity), inclusive instructions: 699 -> 845 (one pow call added). The old code returned before powf. The new code computes the threshold for every pair, so the failure message can print it.
  • Finite passing call: 3 direct calls, 1 malloc, 1 free on main -> 3 / 1 / 1 on the PR.
  • Host function: 1564 B -> 2134 B (nm -S). The release build now has two copies of the code that follows the infinity check (two pow call sites and two throw_rendered call sites, main has one of each). Release binary text: 107635854 B -> 107636622 B (llvm-size, +768 B). The stripped file size does not change (107808480 B). throw! sites: 2 -> 2.

Failure text. Seven inputs that already fail on main were probed with NO_COLOR=1: 0 messages change. The new .not failure for an equal infinite pair uses the existing .not template and ends with Received difference: 0. No new message contains NaN.

Test validity.

  • Release build of main 5c6d4e2, bun test test/js/bun/test/expect.test.js -t "toBeCloseTo\(\)": 10 fail, 19 pass. All 10 are infinite-pair rows, the .resolves / .rejects test, or the message test.
  • bun bd test test/js/bun/test/expect.test.js: 456 pass, 2 todo, 0 fail.
  • The new block also passes under Jest 30.5.2 and Vitest 4.1.9 (28 tests each, the message test is Bun-only).
  • Other suites, run with the debug build: expect-label.test.ts (4 pass), expect-failure-message-angle-brackets.test.ts (2 pass), expect-assertions.test.ts (1 pass), expect-extend.test.js (29 pass).
  • Four single-clause mutations of the fix, selected one at a time in an instrumented debug build and run against the new block: all 4 make at least one test fail. They are: drop same_infinity ||, drop the 0.0 for an equal infinite pair, drop is_infinite(), drop received == expected.

Not changed here.


[human-review] gate passed · iteration 0 · 2 files touched

fails on main (without fix)
ASAN without fix: 10 failed, 2 skipped
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/pr_gate.xml" test/js/bun/test/expect.test.js
bun test v1.4.4 (367d939d9)

test/js/bun/test/expect.test.js:
(pass) expect() > () [155.76ms]
(pass) expect() > toBe() > expect(0).toBe(0) == true [3.48ms]
(pass) expect() > toBe() > expect(0).toBe(0) == true [0.43ms]
(pass) expect() > toBe() > expect(0).toBe(0) == true [0.52ms]
(pass) expect() > toBe() > expect(-0).toBe(-0) == true [0.15ms]
(pass) expect() > toBe() > expect(1).toBe(1) == true [0.16ms]
(pass) expect() > toBe() > expect(1).toBe(1) == true [0.17ms]
(pass) expect() > toBe() > expect(NaN).toBe(NaN) == true [0.15ms]
(pass) expect() > toBe() > expect(Infinity).toBe(Infinity) == true [0.15ms]
(pass) expect() > toBe() > expect({}).toBe({}) == true [0.15ms]
(pass) expect() > toBe() > expect(Symbol(a)).toBe(Symbol(a)) == true [0.17ms]
(pass) expect() > toBe() > expect(0).toBe(false) == false [2.15ms]
(pass) expect() > toBe() > expect(0).toBe("") == false [0.36ms]
(pass) expect() > toBe() > expect(0).toBe(-0) == false [0.20ms]
(pass) expect() > toBe() > expect(0).toBe(-0) == false [0.13ms]
(pass) ex
... (truncated)

release without fix: 2 skipped
bun test v1.4.4-canary.1 (26c2a4b79)

test/js/bun/test/expect.test.js:
(pass) expect() > () [9.33ms]
(pass) expect() > toBe() > expect(0).toBe(0) == true [0.07ms]
(pass) expect() > toBe() > expect(0).toBe(0) == true
(pass) expect() > toBe() > expect(0).toBe(0) == true
(pass) expect() > toBe() > expect(-0).toBe(-0) == true
(pass) expect() > toBe() > expect(1).toBe(1) == true
(pass) expect() > toBe() > expect(1).toBe(1) == true
(pass) expect() > toBe() > expect(NaN).toBe(NaN) == true
(pass) expect() > toBe() > expect(Infinity).toBe(Infinity) == true
(pass) expect() > toBe() > expect({}).toBe({}) == true
(pass) expect() > toBe() > expect(Symbol(a)).toBe(Symbol(a)) == true
(pass) expect() > toBe() > expect(0).toBe(false) == false [0.01ms]
(pass) expect() > toBe() > expect(0).toBe("") == false
(pass) expect() > toBe() > expect(0).toBe(-0) == false
(pass) expect() > toBe() > expect(0).toBe(-0) == false
(pass) expect() > toBe() > expect(1).toBe(2) == false
(pass) expect() > toBe() > expect(1).toBe(true) == false
(pass) expect() > toBe() > expect(1).toBe("1") == false
(pass) expect() > toBe() > expect(Infinity).toBe(-Infinity) == false
(pass) expect() > toBe() > expect("foo
... (truncated)
passes on PR (with fix)
ASAN with fix: 2 skipped
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/pr_gate.xml" test/js/bun/test/expect.test.js
bun test v1.4.4 (367d939d9)

test/js/bun/test/expect.test.js:
(pass) expect() > () [163.00ms]
(pass) expect() > toBe() > expect(0).toBe(0) == true [2.23ms]
(pass) expect() > toBe() > expect(0).toBe(0) == true [0.34ms]
(pass) expect() > toBe() > expect(0).toBe(0) == true [0.11ms]
(pass) expect() > toBe() > expect(-0).toBe(-0) == true [0.08ms]
(pass) expect() > toBe() > expect(1).toBe(1) == true [0.08ms]
(pass) expect() > toBe() > expect(1).toBe(1) == true [0.08ms]
(pass) expect() > toBe() > expect(NaN).toBe(NaN) == true [0.08ms]
(pass) expect() > toBe() > expect(Infinity).toBe(Infinity) == true [0.10ms]
(pass) expect() > toBe() > expect({}).toBe({}) == true [0.08ms]
(pass) expect() > toBe() > expect(Symbol(a)).toBe(Symbol(a)) == true [0.08ms]
(pass) expect() > toBe() > expect(0).toBe(false) == false [6.19ms]
(pass) expect() > toBe() > expect(0).toBe("") == false [0.38ms]
(pass) expect() > toBe() > expect(0).toBe(-0) == false [0.14ms]
(pass) expect() > toBe() > expect(0).toBe(-0) == false [0.11ms]
(pass) ex
... (truncated)

release with fix: 2 skipped
$ bun scripts/build.ts --profile=release
[configured] bun-profile → bun (stripped) in 8363ms (unchanged)
ninja: Entering directory `/workspace/bun/build/release'
[1/310] gen generated_host_exports.rs
generated_host_exports.rs: 116 exports (host=5, lazy=4, generic=107, rust=0); 255 extern-C blocks audited
[2/310] gen cpp.rs (cppbind)
[3/310] gen NodeModuleModule.lut.h
Generating /workspace/bun/build/release/codegen/NodeModuleModule.lut.h from /workspace/bun/src/jsc/modules/NodeModuleModule.cpp
[4/310] gen ZigGlobalObject.lut.h
Generating /workspace/bun/build/release/codegen/ZigGlobalObject.lut.h from /workspace/bun/src/jsc/bindings/ZigGlobalObject.lut.txt
[5/310] gen BunObject.lut.h
Generating /workspace/bun/build/release/codegen/BunObject.lut.h from /workspace/bun/src/jsc/bindings/BunObject.cpp
[6/310] gen .bind.ts → Generated*.{cpp,h}, generated_bindgen_*.rs
[7/308] gen BunProcess.lut.h
Generating /workspace/bun/build/release/codegen/BunProcess.lut.h from /workspace/bun/src/jsc/bindings/BunProcess.cpp
[8/308] gen JS modules (bundle-modules)
Preprocess modules (12345ms)
Bundle modules (534ms)
Postprocesss modules (383ms)
Bundle Functions (806ms)
Generate Code (5
... (truncated)
diff hotspot
src/runtime/test_runner/expect/toBeCloseTo.rs | 22 ++-----
 test/js/bun/test/expect.test.js               | 91 +++++++++++++++++++++++++++
 2 files changed, 97 insertions(+), 16 deletions(-)

gate history · 2 passed · 0 rejected · iteration 0

evidence per changed file
file                                           reads  edits  tests
src/runtime/test_runner/expect/toBeCloseTo.rs      2      2     47
test/js/bun/test/expect.test.js                    2      4     46

… them

`toBeCloseTo` changed each -Infinity operand to +Infinity and then
returned a pass before it read the `.not` flag. So -Infinity was close
to Infinity, and `.not` could not fail for an infinite pair.

Two infinities are now close only when they are equal, and the result
goes through the same `.not` inversion as every other pair.
@robobun

robobun commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 11:30 PM PT - Oct 23rd, 2026

❌ @robobun, your commit d5f9969 has 1 failures in Build #131650 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 46674

That installs a local version of the PR into your bun-46674 executable, so you can run:

bun-46674 --bun

@robobun

robobun commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator Author

Reproduced on a release build of main 5c6d4e2. These four assertions pass there, and Jest 30.5.2 fails all four:

expect(-Infinity).toBeCloseTo(Infinity);
expect(Infinity).toBeCloseTo(-Infinity);
expect(Infinity).not.toBeCloseTo(Infinity);
expect(-Infinity).not.toBeCloseTo(-Infinity);

The new test block shows the same: bun test test/js/bun/test/expect.test.js -t "toBeCloseTo\(\)" gives 10 fail and 19 pass on that build. With this PR, bun bd test test/js/bun/test/expect.test.js gives 456 pass, 2 todo, 0 fail.

CI, build 131650 (the second run, finished): 181 of 182 jobs passed.

  • Every Linux, macOS and Windows 11 aarch64 test job passed. 7 of 8 Windows 2019 x64 test jobs passed.
  • One Windows 2019 x64 job failed. Its only failure is test/js/first_party/ws/ws-proxy.test.ts, in "a message sent during a TLS renegotiation arrives after it". The same test fails the same way on that lane on main, in build 131489. This PR does not change WebSocket or TLS code.
  • test/js/bun/test/expect.test.js ran in jobs that passed on debian 13 x64-asan and on Windows 2019 x64.

I will not push again to rerun CI. The remaining step is a retry of the failed Windows job.

PR: #46674

@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 7d240c1 and 26c2a4b.

Files selected for processing: 2
  • src/runtime/test_runner/expect/toBeCloseTo.rs
  • test/js/bun/test/expect.test.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

Walkthrough

toBeCloseTo() now treats equal positive or negative infinities as a zero-difference match. The result follows the existing .not inversion. Tests cover synchronous and asynchronous comparisons, precision inputs, and failure messages.

Changes

Infinite-value close-to comparisons

Layer / File(s) Summary
Infinity comparison and regression coverage
src/runtime/test_runner/expect/toBeCloseTo.rs, test/js/bun/test/expect.test.js
Equal infinite values produce an actual difference of zero, and the result is subject to .not inversion. Tests cover finite values, infinities, NaN, precision inputs, .resolves, .rejects, and the reported difference for a negated comparison.

Suggested reviewers: dylan-conway

Priority: Low

Merge Risk: Minimal · up to 26c2a

The infinity comparison change appears ready to merge after normal checks.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title names the main change: toBeCloseTo now rejects -Infinity vs Infinity and applies .not to infinite pairs. This matches the raw summary, which says equal infinities are a zero-difference match…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description provides a detailed explanation of the problem, fix, scope, compatibility impact, and verification results. It does not use the exact template headings, but it contains the required in…
Full details: Title check

Explanation

The title names the main change: toBeCloseTo now rejects -Infinity vs Infinity and applies .not to infinite pairs. This matches the raw summary, which says equal infinities are a zero-difference match that goes through the negation flag, and the objectives, which say infinite pairs are close only when equal and go through the usual .not inversion. The title is specific and short enough, though it is long at 88 characters.

Full details: Description check

Explanation

The description provides a detailed explanation of the problem, fix, scope, compatibility impact, and verification results. It does not use the exact template headings, but it contains the required information, including how the change was verified.

Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment on lines +3881 to +3888
try {
expect(Infinity).not.toBeCloseTo(Infinity);
} catch (e) {
message = Bun.stripANSI(ANY(e).message);
}
expect(message).toEndWith("\nReceived difference: 0\n");
});
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) Bun maintainers get a message-shape test whose anchor "\nReceived difference: 0\n" at expect.test.js:3888 is the only guard on the new .not failure body for equal infinities. If message stays undefined because the matcher does not throw, toEndWith fails (expect.rs:1872 sets pass=false for a non-string receiver), so the test is not vacuous. But the assertion only checks the last line; a regression that prints "Received difference: 0" while "Expected difference" or "Expected precision" show "inf"/"NaN" (e.g. precision Infinity) still passes. Fix: assert the full stripped message (or at least every numeric line) with toBe/toMatchInlineSnapshot, and add the precision-Infinity variant so the whole new .not body is pinned.

Why this was flagged

The dismissal is sound on vacuity: run_string_affix_matcher at expect.rs:1872 computes pass = value.is_string(), so expect(undefined).toEndWith(...) throws, and throw_rendered at expect.rs:736 emits {signature}{args} with nothing after the template's trailing "\n", so the anchor is exact. What the test leaves unproven is the rest of the newly reachable .not body at toBeCloseTo.rs:79-80: for expect(Infinity).not.toBeCloseTo(Infinity, Infinity) the body prints "Expected precision: inf" and "Expected difference: < 0", and nothing in the suite pins those lines. The REVIEW.md rule that error messages are reviewed word-for-word and that assertions assert the strongest invariant (toBe over suffix checks) is what this test falls short of. The consequence is limited to test strength on the Bun lane; no production reader is affected beyond the message wording already covered by L1.

Verification: Triggering condition: any future regression in the .not failure body for an equal infinite pair other than its last line goes unnoticed by the suite. The only Bun-specific message test the PR adds is test/js/bun/test/expect.test.js:3879-3887, and its sole assertion is expect(message).toEndWith("\nReceived difference: 0\n") (line 3886).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is right: the test pins only the last line. I left it that way on purpose, for these reasons.

  • The last line is the only part of this message that the PR decides. The failure arms and the template are the same as on main. The new failure prints the body that main already prints for an equal finite pair (expect(1).not.toBeCloseTo(1)), with Infinity as the operands.
  • The assertion fails without the fix. On main nothing throws. With the 0.0 for an equal infinite pair removed, this test fails, because Infinity - Infinity is NaN.
  • An exact match would assert text that the PR body lists under "Not changed here". The .not body has no not in it. At precision Infinity the body is Expected precision: inf, Expected difference: < 0, Received difference: 0. That block reads badly, and I do not want a test that states it as the expected text.
  • No test on main pins any failure text of this matcher today.

The better place for an exact assertion is the change that gives .not.toBeCloseTo failures the body Jest prints (Expected: not Infinity). If a maintainer prefers the full text pinned in this PR, I will add it.

alii added a commit that referenced this pull request Nov 17, 2026
### What does this PR do?

Replaces the matcher part of #32023. Every matcher repeated the same
steps and kept two copies of its message, which had drifted.

- A call is `Uncounted`, then `Matcher`. A built-in matcher returns
`Passed`, which only a `Matcher` makes, so passing uncounted does not
compile. That and a run-time test over all 95 matchers replace the
`expect-call-counter` lint, which searched each matcher file for a
token. Custom matchers and `expect.poll` still count by hand.
- 53 matcher files become 11 (reverses the layout of #21810; overlaps
#46789, #46729 and #48448). `src` is +3,009 / -6,769.
- Deleted: the signature cache, the markup pass on every call,
`PostMatchGuard`, `expect_throw!`, `throw2`, 8 `ensure_still_alive`
calls before the value's last use.
- New: `JSValue::try_for_each`.
- Panic sites: 16 and a `debug_assert!` removed, 0 added. `unsafe`
blocks: 5 removed, 1 added.

**Bugs this fixes**

- A `.not` or `.resolves` read on the same `expect()` while a matcher
ran changed that call, in some matchers only. Those read the flags late.
The call now copies them on entry (`expect.test.js`).
- Panic in `.resolves`/`.rejects` called while another matcher started
to wait. It skipped the pending check. Every call now checks
(`expect-deferred.test.ts`).
- `toBeCloseTo` passed for any two infinities, with or without `.not`.
Same rule as Vitest and #46674 (`expect.test.js`).

**Visible changes**

- A modifier read during a call only affects later calls. Verdicts
change in both directions. Vitest 4.1.9 lets such a read flip the mock
matchers, as main did.
- The nested call of the panic fix always waits, including `pass()`,
`fail()` and argument errors.
- Opposite infinities are not close. `.not` fails on equal ones.

**Kept as is**

- `.not.toThrow(matcher)` never asks the matcher. `toThrow` treats
`expect.any(C)` as `instanceof C` and ignores `{ asymmetricMatch }` (all
#48448).
- `toThrow(expected)` throws a TypeError for a thrown symbol.
- `toThrow` and `.not.toThrow` can both fail: no `message` (#48517),
RegExp without `test`.
- `toBeOneOf(generator)` cannot wait for an async matcher.
- Sticky modifiers (#48073), signature quirks, `inf` in `toBeCloseTo`,
which argument errors count.

**Performance**

| instructions per call, release | before | after |
| --- | --- | --- |
| `toBe` | 1,653 | 1,224 |
| `resolves.toBe` | 2,504 | 2,134 |
| custom matcher | 3,199 | 3,183 |
| six forms of `toThrow` | 38,251 | 34,984 |
| `toContain`, Set of 1,000 | 72,401 | 71,964 |
| `toContainEqual` + `toBeOneOf`, Set of 1,000 | 612,437 | 616,435
(+0.65%) |
| four failing matchers | 91,566 | 89,218 |
| size: linux-x64 / darwin-aarch64 / windows-x64 (CI) | | -40.0 / -32.2
/ -52.0 KB |

### How did you verify your code works?

Final code, Linux x64: 27 test files under debug+ASAN, clippy, format
and source lints. The tests for the three fixes fail on main. The
assertion-count test (6 s) and "can both fail" pass on both. No existing
assertion changed.

A differential against main compared verdict, message, assertion count
and getter/trap order in 5.2 million cells (matcher, value, arguments,
chain, label, flavour, colour). 9,680 differ:

- 80 infinities.
- 8,940 that read a modifier mid-call. 1,428 now pass, 6,328 no longer
do.
- 660 whose snapshot number shifts after such a cell.

Compared with main without that read, only the 80 differ. Snapshot files
are byte-identical.

Failing on both sides: `spyMatchers`, 1 test in `vitest-apis` and 6 in
`snapshot.test.ts` (missing packages), and `error snapshots` (expects
colours).

CI only: other platforms, mordant, the full suite. Jest was not run.
@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: #49164 landed the same rule on main. Opposite infinities are not close, and .not fails on equal ones (src/runtime/test_runner/expect/numbers.rs). All 29 toBeCloseTo() tests of this PR pass on a build of main at bd0219e.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant