Search code examples
testingnestjs

NestJS testing: Cannot find module


When I try to run test:e2e in NestJS, I am getting the error bellow.

Does someone knows the solution? What should I add in package.json?

Thanks.


    Cannot find module 'src/tokens/entities/tokens.entity' from '../src/token-stat/entities/token-stat.entity.ts'

      1 | import { ApiProperty } from '@nestjs/swagger';
    > 2 | import { Token } from 'src/tokens/entities/tokens.entity';
        | ^
      3 | import {
      4 |   Entity,
      5 |   PrimaryGeneratedColumn,

This is my package.json:

   "moduleFileExtensions": [
     "js",
     "json",
     "ts"
   ],
   "moduleNameMapper": {
     "^src/(.*)$": "<rootDir>/$1"
   },
   "rootDir": "./src",
   "testRegex": ".*\\.spec\\.ts$",
   "transform": {
     "^.+\\.(t|j)s$": "ts-jest"
   },
   "collectCoverageFrom": [
     "**/*.(t|j)s"
   ],
   "coverageDirectory": "../coverage",
   "testEnvironment": "node"
 } ```




Solution

  • that's due to how your Jest (not related with Nest at all) is configured. It doesn't know how to resolve absolute paths like that one

    related question: Absolute paths (baseUrl) gives error: Cannot find module

    See: Import Absolute Paths in Typescript Jest Tests.