Skip to content

Improve TestBed error when standalone types are present in "declarations" #45923

@AndrewKushnir

Description

@AndrewKushnir

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

Currently, when standalone component/directive/pipe is present in the "declarations" field of the TestBed.configureTestingModule, the following error is thrown:

Error: Unexpected "MyStandaloneComponent" declaration in "DynamicTestModule" NgModule. 
"MyStandaloneComponent" is marked as standalone and can't be declared in any NgModule - did
you intend to import it?

Which is good, but we can improve it further to mention TestBed.configureTestingModule specifically (since this is an API that developers interact with in this case) instead of DynamicTestModule:

Error: Unexpected `MyStandaloneComponent` found in the `declarations` array during the
`TestBed.configureTestingModule` call. The `MyStandaloneComponent` class is marked as
standalone and can't be declared in any NgModule - did you intend to import it instead (by
adding to the `imports` array)?

Implementation note: we should probably reuse the same function that produces the error, but allow providing some specific details (as a string) about the "origin" of the error (i.e. whether it's a generic error like we have now vs some specific call, e.g. TestBed.configureTestingModule). Basically we can use the proposed error message and in one case we can pass the "in "DynamicTestModule" NgModule" string and in another case - "during the TestBed.configureTestingModule call".

Metadata

Metadata

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimebugcore: error handlingcross-cutting: standaloneIssues related to the NgModule-less world

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions