I am trying to add a custom match to Jest in Typescript. This works fine, but I can't get Typescript to recognize advanced Matchers
.
myMatcher.ts
export default function myMatcher (this: jest.MatcherUtils, received: any, expected: any): { pass: boolean; message (): string; } {
const pass = received === expected;
return {
pass: pass,
message: () => `expected ${pass ? '!' : '='}==`,
}
}
myMatcher.d.ts
declare namespace jest {
interface Matchers {
myMatcher (expected: any): boolean;
}
}
someTest.ts
import myMatcher from './myMatcher';
expect.extend({
myMatcher,
})
it('should work', () => {
expect('str').myMatcher('str');
})
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"moduleResolution": "node",
"module": "es6",
"target": "es5",
"lib": [
"es7",
"dom"
]
},
"types": [
"jest"
],
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist",
"doc",
"**/__mocks__/*",
"**/__tests__/*"
]
}
In someTests.ts I get an error
error TS2339: Property 'myMatcher' does not exist on type 'Matchers'
I read the Microsoft documentation several times, but I can’t figure out how to change the namespace with globally accessible types (not exported).
Putting it in index.d.ts from jest works fine, but is not a good solution for a rapidly changing code base and classes that are extensible by several parties.