0

I created a component using the Angular CLI (quite recent version)

ng --version  
19.2.14

I did this using ng g c foo

The problem I have with this is that the FooComponent is standalone (it should be) but the test file is not. When I run ng test I get the following error:

Unexpected "FooComponent" found in the "declarations" array of the "TestBed.configureTestingModule" call, "FooComponent" is marked as standalone and can't be declared in any NgModule - did you intend to import it instead (by adding it to the "imports" array)?

In order to fix this, I have to move FooComponent from here

TestBed.configureTestingModule({
          declarations: [FooComponent],
          imports: [IonicModule.forRoot()]
        }).compileComponents();

to here

TestBed.configureTestingModule({
          imports: [IonicModule.forRoot(), FooComponent]
        }).compileComponents();

Every time I generate component, service, guard, ...

Why does it do that? Why the inconsistency? Is there a fix?

6
  • plz share package.json, also the global version details of angular cli (ng version) Commented Aug 22 at 10:03
  • 1
    In v19, Angular swapped the default value for standalone from false to true. It means that every component declared is now implicitly standalone. But i think the template for testbed is not uptodate. Commented Aug 22 at 10:04
  • Evidently the CLI hasn't been kept up-to-date - we can't tell you why, or change that; raise an issue with the maintainers. Commented Aug 22 at 10:08
  • Here's the template used by the angular CLI to generate the component and specfile. Feel free to open a PR which updates this file Commented Aug 22 at 11:31
  • I see that it already takes standalone into account, but perhaps it's undefined (which now defaults to true) and takes declarations instead of imports. Swapping the conditional would solve it Commented Aug 22 at 11:35

1 Answer 1

1

The CLI default works fine. (eg. if you create a project with ng new and the run ng g c foo, you'll see that the test file import the standalone component.

It looks like you have some custom configuration for your schematics. (or maybe it's Ionic that's interfering here). Please check your angular.json

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.