Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define custom TS paths for Jest?

I have a monorepo with multiple apps (Angular and NestJS) and they share some things like interfaces, enums, etc. The apps work, and the Jasmine tests with the Angular apps work, but my Jest tests in the NestJS app are failing to import anything from the shared monorepo folder.

Here is my folder structure:

my-repo/
├── ng-ui-1/
├── ng-ui-2/
├── nest-api/
│   ├── src/ (folder w/ app code)
│   ├── tsconfig.json
│   ├── tsconfig.build.json
│   ├── (etc...)
├── monorepo-shared/
│   ├── enums/
│   │   ├── *.enum.ts files
│   ├── interfaces/
│   │   ├── *.interface.ts files

In all the typescript apps I’ve added this to my tsconfig.json files, and it works just fine

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "compilerOptions": {
    //...
    "paths": {
      "@monorepo-shared/constants": ["../monorepo-shared/constants/index"],
      "@monorepo-shared/enums":     ["../monorepo-shared/enums/index"],
      "@monorepo-shared/interfaces":["../monorepo-shared/interfaces/index"]
    }
  }
}

Within a TS file I can just add this to import something from these shared folders, again this works fine everywhere except for Jest tests.

import { BrandingTypeEnum } from '@monorepo-shared/enums'; 

When I run my Jest tests I get this error

Cannot find module '@monorepo-shared/enums' from 'account/account.controller.spec.ts'

In my package.json I define my Jest settings. I know I need to somehow point it towards what @monorepo-shared/enums is since it’s not able to infer that for some reason, but everything I’ve tried so far hasn’t been right.

"jest": {
  "moduleFileExtensions": [
    "js",
    "json",
    "ts"
  ],
  "moduleDirectories": [
    "node_modules",
    "@monorepo-shared/constants",
    "@monorepo-shared/enums",
    "@monorepo-shared/interfaces",
  ],
  "rootDir": "src",
  "testRegex": ".*\\.spec\\.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "collectCoverageFrom": [
    "**/*.(t|j)s"
  ],
  "coverageDirectory": "../coverage",
  "testEnvironment": "node"
},

I’ve tried many of the solutions in this question and nothing has been right so far. What do I need to change to get Jest to see these paths I’ve defined in my tsconfig.json file?

>Solution :

You need to use jest’s moduleNameMapper to tell jest how to resolve @monorepo-shared/enums with respect to the rootDir. Something like this should do it

"jest": {
  "moduleFileExtensions": [
    "js",
    "json",
    "ts"
  ],
  "moduleDirectories": [
    "node_modules",
    "@monorepo-shared/constants",
    "@monorepo-shared/enums",
    "@monorepo-shared/interfaces",
  ],
  "rootDir": "src",
  "testRegex": ".*\\.spec\\.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "collectCoverageFrom": [
    "**/*.(t|j)s"
  ],
  "coverageDirectory": "../coverage",
  "testEnvironment": "node",
  "moduleNameMapper": {
    "@monorepo-shared/enums": ["<rootDir>/../../monorepo-shared/enums/index"],
    "@monorepo-shared/constants": ["<rootDir>/../../monorepo-shared/constants/index"],
    "@monorepo-shared/interfaces":["<rootDir>/../../monorepo-shared/interfaces/index"]
  }
},
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading