From a40cd47aa7ebccfbeeb26e397e03f1372aa10a55 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 2 Feb 2023 09:30:28 +0100 Subject: [PATCH] fix(migrations): avoid modifying testing modules without declarations (#48921) Fixes that we were changing the testing modules that have no `declarations` unnecessarily, resulting in more formatting changes that users would have to clean up. PR Close #48921 --- .../standalone-migration/to-standalone.ts | 5 ++++ .../test/standalone_migration_spec.ts | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) 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 d3f8694fcbb..ebfe985537f 100644 --- a/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts +++ b/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts @@ -437,6 +437,11 @@ function analyzeTestingModules( for (const obj of testObjects) { const declarations = extractDeclarationsFromTestObject(obj, typeChecker); + + if (declarations.length === 0) { + continue; + } + const importsProp = findLiteralProperty(obj, 'imports'); const importElements = importsProp && hasNgModuleMetadataElements(importsProp) ? importsProp.initializer.elements : diff --git a/packages/core/schematics/test/standalone_migration_spec.ts b/packages/core/schematics/test/standalone_migration_spec.ts index 118ec261e16..8079be0d7e6 100644 --- a/packages/core/schematics/test/standalone_migration_spec.ts +++ b/packages/core/schematics/test/standalone_migration_spec.ts @@ -1043,6 +1043,34 @@ describe('standalone migration', () => { `)); }); + it('should not change testing objects with no declarations', async () => { + const initialContent = ` + import {NgModule, Component} from '@angular/core'; + import {TestBed} from '@angular/core/testing'; + import {ButtonModule} from './button.module'; + import {MatCardModule} from '@angular/material/card'; + + describe('bootrstrapping an app', () => { + it('should work', () => { + TestBed.configureTestingModule({ + imports: [ButtonModule, MatCardModule] + }); + const fixture = TestBed.createComponent(App); + expect(fixture.nativeElement.innerHTML).toBe('Hello'); + }); + }); + + @Component({template: 'hello'}) + class App {} + `; + + writeFile('app.spec.ts', initialContent); + + await runMigration('convert-to-standalone'); + + expect(tree.readContent('app.spec.ts')).toBe(initialContent); + }); + it('should migrate tests with a component declared through Catalyst', async () => { writeFile('app.spec.ts', ` import {NgModule, Component} from '@angular/core';