I'm encountering an issue with extending Jest's matchers in TypeScript for custom assertions. Interestingly, while running the tests works fine, the issue arises in Visual Studio Code where TypeScript is not recognizing my custom matchers.
I've tried several approaches to resolve this, but so far, none have worked. I'm hoping to get some insights or suggestions on how to fix this.
Problem:
I've created custom Jest matchers to extend expect for use with AxiosResponse objects. Despite following the standard approach for extending Jest's matcher types, TypeScript is not recognizing my custom matchers.
The TypeScript error in VSCode is:
Property 'status' does not exist on type 'JestMatchers<AxiosResponse<any, any>>'.ts(2339)
Attempts to Resolve:
- Created a custom type declaration file jest-custom-matchers.d.ts with the following content:
import 'jest';
declare global {
namespace jest {
interface Matchers<R> {
status(expectedStatus: number): R;
}
}
}
- Ensured that the
tsconfig.jsonis correctly configured to include the types directory:
{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "./types"],
},
"include": ["src/**/*", "types/**/*"]
}
- Restarted the TypeScript server in VS Code and checked for any conflicting type definitions.
- Moved
.d.tsfile to root as per this thread
Tests run successfully using the command line, but VSCode does not recognize the custom matchers. I would appreciate any insights or suggestions to get TypeScript in VSCode to recognize these custom matchers.
Thank you for your help!