Home > Software engineering >  ts-jest cannot find type information when using monorepo style tests directory in a typescript node
ts-jest cannot find type information when using monorepo style tests directory in a typescript node

Time:10-29

I have separated jest's testing types and project's types by using the references and composite settings. But ts-jest is not able to find the type information in test files and throws multiple errors like: error TS2304: Cannot find name 'expect'.

This is the directory structure:

.
├── package.json
├── src
│   └── index.ts
├── tests
│   ├── index.test.ts
│   └── tsconfig.json
├── tsconfig.json

tsconfig.json:

{
  "compilerOptions": {
    "composite": true,
  },
  "include": ["src"]
}

tests/tsconfig.json:

{
  "extends": "../tsconfig.json",
  "references": [
    {
      "name": "my-lib",
      "path": ".."
    }
  ],
  "compilerOptions": {
    "types": ["jest"],
    "rootDir": "..",
    "noEmit": true
  },
  "include": ["."]
}

package.json:

{
  "jest": {
    "preset": "ts-jest",
    "environment": "node"
  }
}

how to integrate ts-jest in such a projects?

CodePudding user response:

Update jest configuration to define exactly tsconfig file for ts-jest:

package.json

{
  "jest": {
    "preset": "ts-jest",
    "environment": "node",
    "globals": {
      "ts-jest": {
        "tsconfig": "./tests/tsconfig.json"
      }
    }
  }
}
  • Related