diff --git a/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts b/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts index 56d0c314aef..41fa10bfd4b 100644 --- a/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts +++ b/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts @@ -569,7 +569,8 @@ function analyzeTestingModules( const importsProp = findLiteralProperty(obj, 'imports'); const importElements = importsProp && hasNgModuleMetadataElements(importsProp) ? - importsProp.initializer.elements : + // Filter out calls since they may be a `ModuleWithProviders`. + importsProp.initializer.elements.filter(el => !ts.isCallExpression(el)) : null; for (const decl of declarations) { diff --git a/packages/core/schematics/test/standalone_migration_spec.ts b/packages/core/schematics/test/standalone_migration_spec.ts index 676f2e01f77..090652a9b93 100644 --- a/packages/core/schematics/test/standalone_migration_spec.ts +++ b/packages/core/schematics/test/standalone_migration_spec.ts @@ -1095,6 +1095,47 @@ describe('standalone migration', () => { `)); }); + it('should not add ModuleWithProviders imports to the `imports` in a test', async () => { + writeFile('app.spec.ts', ` + import {NgModule, Component} from '@angular/core'; + import {TestBed} from '@angular/core/testing'; + import {MatCardModule} from '@angular/material/card'; + + describe('bootrstrapping an app', () => { + it('should work', () => { + TestBed.configureTestingModule({ + declarations: [App], + imports: [MatCardModule.forRoot({})] + }); + const fixture = TestBed.createComponent(App); + expect(fixture.nativeElement.innerHTML).toBe('hello'); + }); + }); + + @Component({template: 'hello'}) + class App {} + `); + + await runMigration('convert-to-standalone'); + + const content = stripWhitespace(tree.readContent('app.spec.ts')); + + expect(content).toContain(stripWhitespace(` + @Component({template: 'hello', standalone: true}) + class App {} + `)); + + expect(content).toContain(stripWhitespace(` + it('should work', () => { + TestBed.configureTestingModule({ + imports: [MatCardModule.forRoot({}), App] + }); + const fixture = TestBed.createComponent(App); + expect(fixture.nativeElement.innerHTML).toBe('hello'); + }); + `)); + }); + it('should not change testing objects with no declarations', async () => { const initialContent = ` import {NgModule, Component} from '@angular/core';