【问题标题】:ts-jest cannot find type information when using monorepo style tests directory in a typescript node project在 typescript 节点项目中使用 monorepo 样式测试目录时,ts-jest 找不到类型信息
【发布时间】:2021-10-29 09:56:42
【问题描述】:

我使用referencescomposite 设置分隔了jest 的测试类型和项目类型。但是 ts-jest 无法在测试文件中找到类型信息并抛出多个错误,例如:error TS2304: Cannot find name 'expect'

这是目录结构:

.
├── 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"
  }
}

如何在这样的项目中集成 ts-jest?

【问题讨论】:

    标签: node.js typescript testing jestjs ts-jest


    【解决方案1】:

    更新 jest 配置以准确定义 ts-jest 的 tsconfig 文件:

    package.json

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

    【讨论】:

      猜你喜欢
      • 2019-01-16
      • 2018-12-11
      • 1970-01-01
      • 2020-06-06
      • 2021-07-16
      • 2021-01-19
      • 2020-04-03
      • 2019-01-18
      • 2019-11-15
      相关资源
      最近更新 更多