Support asymmetric matchers with equals in expect.extend (#10602)

This commit is contained in:
Keigo Ando
2024-04-30 02:37:35 +09:00
committed by GitHub
parent 2bf3f32fb8
commit 7062e89d2e
3 changed files with 16 additions and 1 deletions

View File

@@ -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));
}
};

View File

@@ -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();
});

View File

@@ -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;