mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Support asymmetric matchers with equals in expect.extend (#10602)
This commit is contained in:
@@ -4977,7 +4977,7 @@ pub const ExpectMatcherContext = struct {
|
||||
return .zero;
|
||||
}
|
||||
const args = arguments.slice();
|
||||
return JSValue.jsBoolean(args[0].deepEquals(args[1], globalObject));
|
||||
return JSValue.jsBoolean(args[0].jestDeepEquals(args[1], globalObject));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -50,6 +50,9 @@ expect.extend({
|
||||
|
||||
return { message, pass };
|
||||
},
|
||||
_toCustomEqual(actual, expected) {
|
||||
return { pass: this.equals(actual, expected) };
|
||||
},
|
||||
|
||||
// this matcher has not been defined through declaration merging, but expect.extends should allow it anyways,
|
||||
// type-enforcing the generic signature
|
||||
@@ -340,3 +343,14 @@ it("should propagate errors from calling .toString() on the message callback val
|
||||
"i have successfully propagated the error message!",
|
||||
);
|
||||
});
|
||||
|
||||
it("should support asymmetric matchers", () => {
|
||||
expect(1)._toCustomEqual(expect.anything());
|
||||
expect(1)._toCustomEqual(expect.any(Number));
|
||||
expect({ a: "test" })._toCustomEqual({ a: expect.any(String) });
|
||||
expect(() => expect(1)._toCustomEqual(expect.any(String))).toThrow();
|
||||
|
||||
expect(1).not._toCustomEqual(expect.any(String));
|
||||
expect({ a: "test" }).not._toCustomEqual({ a: expect.any(Number) });
|
||||
expect(() => expect(1).not._toCustomEqual(expect.any(Number))).toThrow();
|
||||
});
|
||||
|
||||
1
test/js/bun/test/expect-extend.types.d.ts
vendored
1
test/js/bun/test/expect-extend.types.d.ts
vendored
@@ -8,6 +8,7 @@ interface CustomMatchersForTest {
|
||||
_toBeDivisibleBy(value: number): any;
|
||||
_toBeSymbol(value: Symbol): any;
|
||||
_toBeWithinRange(floor: number, ceiling: number): any;
|
||||
_toCustomEqual(value: any): any;
|
||||
_shouldNotError(): any;
|
||||
_toFailWithoutMessage(): any;
|
||||
_toBeOne(): any;
|
||||
|
||||
Reference in New Issue
Block a user