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
This commit is contained in:
Kristiyan Kostadinov 2023-02-02 09:30:28 +01:00 committed by Dylan Hunn
parent 8a9907c23a
commit a40cd47aa7
2 changed files with 33 additions and 0 deletions

View file

@ -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 :

View file

@ -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>Hello</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';