在 yarn workspace 的子包中找不到 jest

Can not find jest in subpackage of yarn workspace

我正在尝试将 Typescript 项目重构为具有多个包(带有 yarn 工作区)的 monorepo。在其中一个软件包 (packages/compiler) 中,我已经安装了 jest,并进行了多项测试。他们以前工作过,但重构后我收到以下错误消息:

Cannot find name 'it'. Do you need to install type definitions for a

test runner? Try npm i @types/jest or npm i @types/mocha

我似乎不知道如何让笑话发挥作用。

我开玩笑地在包含所有测试的包中“本地”安装了它。但是如果我在 yarn install 之后检查 node_modules/,我只能在 packages/compiler/node_modules 中看到一个 .bin 目录,并且我可以看到 (ts-)jest 安装在“root”中node_modules。我认为我需要确保将 jest 安装在我的包 (packages/compiler/node_modules) 的 node_modules 而不是根目录中。但我似乎不知道该怎么做(根 package.json 中的 nohoist 选项不起作用)。

所以我的问题是如何让 jest 在我的子包中工作?

package.json:

{
  "name": "pannenkoekjs",
  "private": true,
  "workspaces": [
    "packages/*"
  ],
  "devDependencies": {
    "lerna": "^3.18.4"
  }
}

packages/compiler中的package.json:

{
  "name": "@pannenkoekjs/compiler",
  "dependencies": {
    "typescript": "^3.6.2"
  },
  "devDependencies": {
    "@types/jest": "^24.0.23",
    "jest": "^24.9.0",
    "ts-jest": "^24.1.0",
  }
}

packages/compiler中的jest.config.js

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
};

显然错误不是因为它没有找到笑话,而是类型定义。我安装了 @types/jest,但它安装在 "root" node_modules 中。在我的 tsconfig.jsonpackages/compiler 中,我有以下内容:

"typeRoots": [
    "./node_modules/@types",
    "./src/@types",
]

换句话说;它正在该包的本地 node_modules 中查找类型定义(因此 packages/compiler/node_modules),而 jest 的类型定义安装在根目录中。所以将其添加到 typeRoots 解决了我的问题:

"typeRoots": [
    "./node_modules/@types",
    "../../node_modules/@types",
    "./src/@types",
]