Home > database >  Cannot find module '@ngrx/effects' when running tests with Jest
Cannot find module '@ngrx/effects' when running tests with Jest

Time:04-28

When trying to run tests with jest in our angular application I get the following message:

Cannot find module '@ngrx/effects' from 'src/app/modules/core/core.module.ts'

It is weird, because the application runs and resolves this module without a problem, this error also appears when I try to import this module in our setup-jest.ts. It seems like jest's resolver is the one that can't find this.

Snippets from the app's config

package.json:

"dependencies": {
"@angular/animations": "~12.1.2",
"@angular/cdk": "^12.1.2",
"@angular/common": "~12.1.2",
"@angular/compiler": "^12.1.2",
"@angular/core": "~12.1.2",
"@angular/forms": "~12.1.2",
"@angular/material": "^12.1.2",
"@angular/platform-browser": "~12.1.2",
"@angular/platform-browser-dynamic": "~12.1.2",
"@angular/router": "~12.1.2",
"@angular-devkit/build-angular": "^12.1.2",
"@angular-eslint/builder": "12.3.1",
"@angular-eslint/eslint-plugin": "12.3.1",
"@angular-eslint/eslint-plugin-template": "12.3.1",
"@angular-eslint/schematics": "12.3.1",
"@angular-eslint/template-parser": "12.3.1",
"@angular/cli": "~12.1.2",
"@angular/compiler-cli": "^12.1.2",
"@ngrx/schematics": "^13.0.2",
"@ngrx/effects": "^13.1.0",
"@ngrx/store": "^12.5.1",
"@ngrx/store-devtools": "^12.5.1",
"@ngx-translate/core": "^13.0.0",
"@ngx-translate/http-loader": "^6.0.0",
...

jest config (also in package.json)

"jest": {
"preset": "jest-preset-angular",
"setupFilesAfterEnv": [
  "<rootDir>/setup-jest.ts"
],
"testPathIgnorePatterns": [
  "<rootDir>/node_modules/",
  "<rootDir>/dist/"
],
"moduleDirectories": [
  "node_modules"
],
"moduleNameMapper": {
  "^@environments/(.*)": "<rootDir>/src/environments/$1",
  "^@app/(.*)": "<rootDir>/src/app/$1"
},
"testMatch": [
  "**/__tests__/**/!(DISABLED.)*.[jt]s?(x)",
  "**/!(DISABLED.)?(*.) (spec|test).[tj]s?(x)"
],
"globals": {
  "ts-jest": {
    "tsConfig": "<rootDir>/tsconfig.spec.json",
    "stringifyContentPathRegex": "\\.html$"
  }
} 
...

The require stack is the following:

in our app module imports:

StoreModule.forRoot(reducers),
EffectsModule.forRoot([]),
...

in our core module imports:

StoreModule.forFeature(fromCore.coreFeatureKey, fromCore.coreReducer),
EffectsModule.forFeature([CoreEffects])
...

and finally, our device module that imports the core module:

 imports: [
    CoreModule,
    ...

The error: console output of the error

What I've tried:

  • explicitly tell jest where to resolve @ngrx/effects in node_modules
  • update jest version to the latest
  • tried to import @ngrx/effects in setup-jest.ts (yielded same error)
  • weep, a lot

I am fairly new to angular and it's testing, I tried to look up previous answers and rechecked my angular and ngrx courses, but have no idea what the problem could be. Any ideas or comments are greatly appreciated! Thank you in advance!

CodePudding user response:

Please try keeping all the angular and ngrx versions the same. In this case, download ngrx effects to v12.

  • Related