You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jest报“组件属于两个模块声明”错误的原因排查

Angular(NX)项目测试时的组件重复声明异常

异常信息

Error: Type FooComponent is part of the declarations of 2 modules: FooComponentModule and FooComponentModule! Please consider moving FooComponent to a higher module that imports FooComponentModule and FooComponentModule. You can also create a new NgModule that exports and includes FooComponent then import that NgModule in FooComponentModule and FooComponentModule.

问题场景

  • 仅在MacOS系统的IntelliJ 2022.3.1内置Jest集成中触发该错误;Windows系统的同事在所有环境下都会遇到
  • 测试目标为app.component.spec.ts,该测试在CLI中执行正常,甚至复制IntelliJ生成的执行命令到终端也能成功运行

排查进展

  • 初步判定为NX相关问题:创建包含lib、路径别名等配置的纯Angular+Jest项目,未出现同类异常
  • 已在NX官方仓库提交对应问题反馈

内容的提问来源于stack exchange,提问作者iceteabottle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 15:55:25