Skip to content

ng g @angular/core:standalone should not move RouterTestingModule.withRoutes() import #48971

@cexbrayat

Description

@cexbrayat

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

core

Is this a regression?

No

Description

The following unit test that uses a test component, and RouterTestingModule.withRoutes([]) in configureTestingModule:

import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

@Component({
  template: '<div>Hello World</div>'
})
class TestComponent { }

describe('AppComponent', () => {
  beforeEach(() => TestBed.configureTestingModule({
    declarations: [TestComponent],
    imports: [RouterTestingModule.withRoutes([])]
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(TestComponent);
    expect(fixture.nativeElement.querySelector('div').textContent).toContain('Hello World');
  });
});

After ng g @angular/core:standalone --defaults,
it gets migrated to:

import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

@Component({
    template: '<div>Hello World</div>',
    standalone: true,
    imports: [RouterTestingModule.withRoutes([])]
})
class TestComponent { }

describe('AppComponent', () => {
  beforeEach(() => TestBed.configureTestingModule({
    imports: [RouterTestingModule.withRoutes([]), TestComponent]
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(TestComponent);
    expect(fixture.nativeElement.querySelector('div').textContent).toContain('Hello World');
  });
});

Note that RouterTestingModule.withRoutes([]) is still in the testing module, but all added to the imports of the standalone component, which then fails ng test.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Error: src/app/app.component.spec.ts:8:15 - error TS2322: Type 'ModuleWithProviders<RouterTestingModule>' is not assignable to type 'readonly any[] | Type<any>'.

8     imports: [RouterTestingModule.withRoutes([])]

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 15.2.0-next.3
Node: 16.17.0
Package Manager: npm 8.19.3
OS: darwin arm64

Angular: 15.2.0-next.3
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1502.0-next.3
@angular-devkit/build-angular   15.2.0-next.3
@angular-devkit/core            15.2.0-next.3
@angular-devkit/schematics      15.2.0-next.3
@schematics/angular             15.2.0-next.3
rxjs                            7.8.0
typescript                      4.9.5

Anything else?

No response

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: migrationsIssues related to `ng update`/`ng generate` migrationsbugcross-cutting: standaloneIssues related to the NgModule-less worldstate: has PR

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions