#VSCode TypeScript Issue with Extending Jest Matchers

1 messages · Page 1 of 1 (latest)

polar anvil
#

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:

  1. 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;
    }
  }
}
  1. Ensured that the tsconfig.json is correctly configured to include the types directory:
{
  "compilerOptions": {
    "typeRoots": ["./node_modules/@types", "./types"],
  },
  "include": ["src/**/*", "types/**/*"]
}
  1. Restarted the TypeScript server in VS Code and checked for any conflicting type definitions.
  2. Moved .d.ts file 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!

polar anvil
#

Resolved it. I had to add the specs directory in the include and that did the trick!