Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not find jest in subpackage of yarn workspace

I'm trying to refactor a Typescript project to monorepo with multiple packages (with yarn workspaces). In one of the packages (packages/compiler) I have jest installed, and have several tests. They worked before, but after refactoring I get the following error message:

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

I can't seem to figure out how to get jest to work.

I have jest installed "locally" in the package with all the tests. But if I check node_modules/ after a yarn install, I can only see a .bin directory in packages/compiler/node_modules, and I can see (ts-)jest installed in the "root" node_modules. I think that I need to make sure that jest is installed in the node_modules of my packages (packages/compiler/node_modules) instead of in the root. But I can't seem to work out how to do that (the nohoist option in the root package.json doesn't work).

So my question is how do I get jest to work in my subpackage?

The root package.json:

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

The package.json in packages/compiler:

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

And the jest.config.js in packages/compiler:

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
};
like image 920
Wessel van der Linden Avatar asked Nov 14 '19 10:11

Wessel van der Linden


People also ask

Can I use yarn workspaces with npm?

The package. json in the root has a new “workspaces” section which tells yarn or npm where the packages are. With this in place, you only need to run npm install or yarn install once, in the root of the directory.

How do yarn workspaces work?

Yarn Workspaces is a feature that allows users to install dependencies from multiple package. json files in subfolders of a single root package. json file, all in one go. Yarn can also create symlinks between Workspaces that depend on each other, and will ensure the consistency and correctness of all directories.

What is yarn Monorepo?

Monorepos and Yarn A monorepo allows multiple applications to coexist in the same repository and cross-reference each other, easing the overhead of repository management and allowing a higher degree of collaboration among teams. Yarn is an alternative package manager to npm.


1 Answers

Apparently the error wasn't about it not finding jest, but the type definitions. I have @types/jest installed, but it is installed in de "root" node_modules. And in my tsconfig.json in packages/compiler I had te following:

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

In other words; it was looking for type definitions in the local node_modules of that package (so packages/compiler/node_modules), while jest's type definitions were installed in the root. So adding that to the typeRoots fixed my issue:

"typeRoots": [
    "./node_modules/@types",
    "../../node_modules/@types",
    "./src/@types",
]
like image 118
Wessel van der Linden Avatar answered Sep 28 '22 03:09

Wessel van der Linden