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
相关产品推荐
相关产品推荐

