TypeScript Answers

How to fix the “Cannot find name ‘it'” error in Jest and TypeScript?

Spread the love

To fix the "Cannot find name ‘it’" error in Jest and TypeScript, we add the ts-jest transform to our test files by adding the entry in jest.config.js.

We install ts-jest by running

npm install jest @types/jest ts-jest

For instance, we write

module.exports = {
  roots: ["<rootDir>/src"],
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],

in jest.config.ts to transform "^.+\\.tsx?$" files with ts-jest.

Then in tsconfig.json, we add

  "compilerOptions": {

    "types": ["reflect-metadata", "jest"],
    "typeRoots": ["./types", "./node_modules/@types"]

  "exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"],
  "include": ["./src/**/*.tsx", "./src/**/*.ts"]

to add the 'jest' type to the types option.

Now the TypeScript compiler should be aware of Jest types.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *