Repository navigation
Conversation
…method The objects that expect.any(), expect.anything(), expect.arrayContaining(), expect.closeTo(), expect.objectContaining(), expect.stringContaining() and expect.stringMatching() return had no asymmetricMatch(). A matcher of your own could not ask them, and code that tells a matcher by that method, such as equals() of @jest/expect-utils, took them for plain values. One schema const gives the seven classes the method, through the trait AsymmetricMatcher. It returns what deep equality decides for the matcher as the expected value, which is what toEqual() runs. The answer goes to the caller at once, so a matcher of expect.resolvesTo or expect.rejectsTo reads a settled promise and throws for a pending one. As in Jest and Vitest the method is not enumerable and is configurable. A copy of a matcher that keeps its prototype, as jest-matcher-utils makes one before it prints a diff, is no matcher: there the method answers undefined and does not throw.
…e toEqual() A matcher of expect.extend() or expect.toSatisfy() had the method already. It handed the value to the matcher function as it was, so for a matcher from expect.resolvesTo or expect.rejectsTo it disagreed with toEqual(), which reads the promise first: a settled promise was not unwrapped, a value that is no promise could match, and a pending promise got an answer. The class now takes the method of the built-in matchers. With a promise flag it returns what deep equality decides and throws for a pending promise. Without one it calls the matcher function as before. The method is no longer enumerable, is configurable, and answers undefined for a `this` that is no matcher.
Collaborator
Author
|
Status: draft, stacked on #47996. How to reproduce the problem on main: import { expect } from "bun:test";
console.log(typeof expect.any(Number).asymmetricMatch); // "undefined". Jest 30.5.2 and Vitest 5.1.0 print "function".
expect.any(Number).asymmetricMatch(1); // TypeError: expect.any(Number).asymmetricMatch is not a functionWithout the change to CI:
|
Collaborator
Author
This was referenced Nov 11, 2026
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Stacked on #47996. Draft until it merges.
Problem
expect.any()and the six other built-in matchers have noasymmetricMatch(), so your own matcher cannot ask one:TypeError: expect.any(Number).asymmetricMatch is not a function.equals(1, expect.any(Number))of@jest/expect-utilsisfalse: it looks for that method. No user reported this.src/runtime/test_runner/expect.bind.ts:17).Fix
received.jest_deep_equals(matcher), the call oftoEqual(): 342 of 342 probed cells agree. AresolvesToorrejectsTomatcher throws for a pending promise.test/js/bun/test/expect.test.js(18 tests, 16 fail without the fix). Alsoexpect-extend,jest-extended,mock-fn,inspect,bun-types.AsymmetricMatchertype has it).Background
toEqual(). Jest and Vitest ask it throughmatcher.asymmetricMatch(value).expect.extend(). Its method did not read a promise first.expect.arrayContaining()with its sample #47996,Bun.inspect(expect.arrayContaining([1]))lists the new method.bindings.cpp(same verdicts, 51 more lines of C++) and a base class (expect.anything()grows by 16 bytes).Downsides
expect.extend()matchers fromexpect.resolvesToorexpect.rejectsTo: the method changes in 12 of 42 cells.for...inno longer lists it..text+1,120 B,.rodata+4,352 B (size -A,--lto=off). Per global: +1 function per class.toEqual()with a matcher: 1,727 instructions before, 1,725 after.expect.toSatisfy(fn).asymmetricMatch(1): 3,924 to 3,927, run spread 12.Notes
What changes for existing tests
expect.extend()andexpect.toSatisfy()matchers made throughexpect.resolvesTo/expect.rejectsTo(also behindexpect.not):asymmetricMatch()now unwraps a settled promise, answersfalsefor a value that is not a promise, and throwsAn async matcher only works inside an expect() matcher, which then returns a promise: await expect(received).toEqual(expected)for a pending promise. Before, the matcher function got the promise itself. 12 of 42 cells change per matcher (6 chains by 7 values): 8 answers flip and 4 pending promises throw. Without a promise flag 0 of 14 cells change. Example:expect.resolvesTo.toBeOdd().asymmetricMatch(Promise.resolve(1))wasfalseand istrue.toEqual()does.ExpectCustomAsymmetricMatcher.prototype.asymmetricMatchhas shipped since 1.0.15 as enumerable and not configurable. It is now not enumerable and configurable.thisthat is no matcher of its class the method answersundefined. Theexpect.extend()one threwExpected this to be instanceof ExpectCustomAsymmetricMatcher.printDiffOrStringifyof jest-matcher-utils 30.5.2 calls the method on such a copy: of 20 probed cases 2 throw on main and 0 throw here.Object.create(Object.getPrototypeOf(matcher))), and the prototype itself, now have the method.toEqual()takes such an object for a matcher that matches nothing, andBun.inspect()listsasymmetricMatchon the copy. On main it compares as an empty object and prints asExpectAny {}.Bun.inspect()prints for a matcher changes: 36 rows are the same with and without this PR (9 matchers fromexpect,expect.not,expect.resolvesToandexpect.rejectsTo, each plain, nested and withshowHidden).The contract
matcher.asymmetricMatch(value)compares astoEqual()does. It is not the answer of Jest in every cell, because bun'stoEqual()already differs from the method of Jest 30.5.2 in some. Four examples, bun then Jest:expect.arrayContaining([])on"x"(false,true),expect.closeTo(1)onnew Number(1)(false,true),expect.stringContaining("a")onnew String("a")(false,true),expect.not.objectContaining({ a: 1 })on1(true,false). The method inherits these.toEqual()still asks a matcher ofexpectby its class. It does not call a function that a test stored over the method: aftermatcher.asymmetricMatch = () => trueon anexpect.any(Number),expect("x").toEqual(matcher)still fails.expect.addEqualityTesters(), also underimport ... from "vitest". Vitest 5.1.0 reads only its second argument there. The method takes one argument (length1, Vitest has 2 forarrayContainingandobjectContaining).$$typeof,sample,inverse,toString(),toAsymmetricMatcher(),getExpectedType(). A library that prints with its own printer still sees a plain object.diff(expect.any(Number), 1)returnsnullwhere it returnedComparing two different types of values.diffof@vitest/utilsreturnsundefinedwhere it returned- Expected: {}/+ Received: 1.calledWith(expect.any(Number))of vitest-mock-extended 6.0.0 and of vitest-when 1.0.0 now matches.calledWith()of jest-mock-extended 4.0.2 still matches nothing: it also needs thejest.fn(undefined)fix of bun test: follow-ups to Vitest compatibility #47727. jest-when 4.0.3 does not load with or without this PR.Measurements
Release builds with
--lto=offof main 89452d2 with #47996 (before), and of the same tree with this PR (after). linux x64.size -A):.text+1,120 B,.rodata+4,352 B,.eh_frame+80 B. The stripped file grows by 4,096 B.nm -S):to_equal_verdict1 copy, 177 B. 7 new Rust thunks of 56 B. 8 host functions of 70 B, 7 of them new (the one of theexpect.extend()class was 79 B). 7 new prototype tables of 40 B. The Rust thunk of theexpect.extend()class: 176 B to 201 B.GeneratedExpect.cpp: 154 to 161.asymmetricMatchentries in the table of built-ins of the ahead-of-time compiler: 1 to 8 (7,240 to 7,247 entries).perf_event_openinstructions:u,BUN_JSC_useJIT=0, (I(2N) - I(N)) / N), before and after:expect(1).toEqual(matcher)withexpect.any(Number)made once: 1,727 and 1,725.expect(1).toEqual(expect.any(Number)): 2,284 and 2,278.expect({ a: [1, 2] }).toEqual({ a: matcher })withexpect.arrayContaining()made once: 3,019 and 3,017.expect.any(Number): 683 and 683.expect.toSatisfy(fn).asymmetricMatch(1): 3,924 and 3,927. 7 runs of one binary spread over 12.expect.any(Number).asymmetricMatch(1): 484 after (600 with the loop, 116 for the empty loop).Bun.inspect(new net.SocketAddress(..)): 21,525 and 21,525.src/jsc/bindings/bindings.cpp: 0 lines changed.toEqual()runs the code that it ran.bun -e 0, 10 runs: 4,433,796 to 4,470,090 instructions before, 4,435,966 to 4,454,050 after.bun teston one file with six matchers (counted withptrace): 279 to 280 before, 277 to 286 after.heapStats().objectTypeCounts): +0 per matcher object. Per global, on the first object of a class: +1Functionand +1NativeExecutable. Per call of a built-in method: 0.expect.resolvesTo.any(Number).asymmetricMatch(1): 1Promiseper call. The method of theexpect.extend()class: 2 cells per call before and after.toEqual(): 0 of 342 cells differ (9 matchers by 6 chains by 7 values, without the 36 cells wheretoEqual()waits and the method throws).bloaty,valgrind,perfandstraceare not installed here. The two counters are small C programs overperf_event_openandptrace.bun build --compile --aot, astest/bundler/compile-aot-size.test.tsprints them in CI (LTO, linux x64), for the base branch and then for this PR: the compiler takes 23,663,539 and 23,663,789 bytes.hello worldmaps 68,117,555 and 68,117,683 bytes.hello world --trimmaps 25,986,099 and 25,973,939 bytes. The bound of that test for--trimis 25,880,000 bytes, so it fails on the base branch and here.Tests
expect.test.js, 18 tests: the answers of 17 matchers against values that match and values that do not, each also throughtoEqual(). The descriptor. A matcher of your own that hands the value on,allOfandanyOf. Code that looks for the method. Matchers inside a sample. No assertion counted. Errors thrown while it matches. Anotherthis. 252 cells of 7 matchers from 6 chains againsttoEqual(), and 72 cells forexpect.extend()andexpect.toSatisfy(). Pending promises. A child process with an equality tester and with rejections that nothing else handles. WhatBun.inspect()prints. Every function ofexpect.not.test/integration/bun-types/fixture/test.tstype-checks the docs example.expect-extend,expect-extend-asymmetric-match-throw,expect-extend-matcher-utils-throw,expect-extend-preload,jest-extended,mock-fn,spyMatchers,expect-label,expect-toHaveReturnedWith,pretty-format-overflow,pretty-format-overridden-size,expect-stack-overflow-crash,expect-assertions,12650,inspect.test.js, and fromtest/aot/builtins:differential/operands,differential/wrong-columns,own-properties,module-exports,proven-returns,name-classes,classes-with-inspect-method.--lto=off):test/aot/builtins/differential.test.tspasses (6,567 tests, the 8asymmetricMatchentries among them), and so donative-contract.test.tsfor theExpectentries andexpect.test.js. On main with bun:test: printexpect.arrayContaining()with its sample #47996 and this PR,differential.test.tspasses too (6,953 tests).Open PRs on the same lines
expect.arrayContaining()with its sample #47996 is the base of this branch. If it is declined, an arm that keepsExpectArrayContaining {}does the same for this PR.console.logformatter #46729 movesprint_asymmetric_matchertoexpect.rsand has no arm forExpectArrayContaining. The one of bun:test: printexpect.arrayContaining()with its sample #47996 and Print Bun's snapshot format with theconsole.logformatter #46729 that merges second must carry the arm. Without itBun.inspect(expect.arrayContaining([1]))lists the method, and the test "the method is not printed with a matcher" fails.JSValue::jest_asymmetric_match. If it merges first,to_equal_verdictcan call it. Both PRs add a paragraph after the same example indocs/test/writing-tests.mdx.Pass::barrier()toPass::barrier(None): one line of conflict inexpect.rs.