简体   繁体   中英

Why jest fails with "component is part of the declaration of 2 modules"?

I'm getting the following exception within executing a test in an angular (nx) project:

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.

The thing is I'm getting this exception only in IntelliJ (2022.3.1, MacOS) when I use the build-in jest integration. A colleague with Windows is getting this exception everywhere.

I was trying to execute the test app.component.spec.ts in my test project . The test should be executed successfully – everywhere not only in the CLI. The craziest thing is: I copied the command IntelliJ is executing within the build-in jest integration to the build-in terminal and it just works

I'm really unsure where to address this issue. Is this an issue with jest? Angular? NX? IntelliJ?

Update:

It seems to be an nx related issue. I created a simple clean angular project with jest and the same prerequisites (like lib, path aliases) and everything worked as expected.

Update 2:

I created an issue in the nx repo: https://github.com/nrwl/nx/issues/14494

It's not really the solution of the main problem but to use standalone components instead of SCAM Components would help and avoid the exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM