jestjsnestjsnestjs-testing

Nestjs e2e testing of an application within a monorepo fails to resolve @app import from library with jest despite config in package.json


I'm trying to run e2e tests for a monorepo application that also utilises several libraries from within the monorepo, all imports throughout the application are resolved using "@app" imports, for example import { ConfigService } from "@app/config"

However, when trying to run e2e tests via the command:

"test:public": "jest --config ./apps/public-microservice/test/jest-e2e.json",

Jest throws:

 Cannot find module '@app/config' from '../src/public-microservice.module.ts'

I've looked at this demo-repo from @jmcdo29 and can't find anything that is different with my configuration.

I've noticed there was an issue about wrong configurations being generated via jest here in 2019, but it has long been resolved, and my configuration for jest in package.json does indeed mention:

    "moduleNameMapper": {
      "@app/config/(.*)": "<rootDir>/libs/config/src/$1",
      "@app/config": "<rootDir>/libs/config/src",

whilst the local targeted file by the package.json script command only contains:

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  }
}

Is there something that's missing from my command or my configuration?

Is there anything I need to specify to tell jest to extend the configuration for jest available in package.json?

Any help investingating this is appreciated, thanks.


Solution

  • I added the "moduleNameMapper" key to my jest e2e suite config and updated the "../" relative paths to match where my libs are, in my specific scenario, it looks like this:

      "moduleNameMapper": {
        "@app/config/(.*)": "<rootDir>../../../libs/config/src/$1",
        "@app/config": "<rootDir>../../../libs/config/src",
      },