找不到模块'@ngrx/效果'开玩笑进行测试时

发布于 2025-01-24 13:55:36 字数 2687 浏览 0 评论 0原文

当尝试在我们的角度应用程序中使用jest进行测试时,我会收到以下消息:

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

这很奇怪,因为应用程序运行并解决此模块而没有问题,当我尝试在我们的设置中导入此模块时,此错误也会出现-jest.ts 。似乎开玩笑的架子架是找不到的。

来自应用程序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(也在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$"
  }
} 
...

require stack如下:

在我们的app模块导入中:

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

在我们的核心模块Imports中:

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

最后,导入核心模块的我们的设备模块:

 imports: [
    CoreModule,
    ...

错误: 错误的控制台输出

我尝试过的内容:

  • 明确地告诉开玩笑在node_modules中的ngrx/效果 @node_modules中的效果
  • 更新开玩笑版本,
  • 试图在setup-jest.ts中导入 @ngrx/效果(产生相同的错误)
  • 哭泣,

我对Angular是相当陌生的,我的测试很新,我尝试过,我尝试了查找以前的答案并重新检查了我的角度和NGRX课程,但不知道问题可能是什么。任何想法或评论都非常感谢!先感谢您!

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
quot;
  }
} 
...

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦一生花开无言 2025-01-31 13:55:36

请尝试将所有角和NGRX版本保持不变。
在这种情况下,将NGRX效果下载到V12。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文