I am trying to add a custom matcher to Jest in Typescript. This works fine, but I can\'t get Typescript to recognize the extended Matchers
.
myMatcher.ts
Answer by @AluanHaddad is almost correct, sans a few types. This one works:
export {};
declare global {
namespace jest {
interface Matchers {
myMatcher: (received: string) => R;
}
}
}
function myMatcher(this: jest.MatcherUtils, received: string, expected: string): jest.CustomMatcherResult {
const pass = received === expected;
return {
pass,
message: (): string => `expected ${received} to be ${expected}`,
}
}
expect.extend({
myMatcher,
});
For a real-world example, see https://github.com/Quantum-Game/quantum-tensors/blob/master/tests/customMatchers.ts (and that the tests actually pass: https://travis-ci.com/Quantum-Game/quantum-tensors).