Skip to content

Commit a40cd47

Browse files
crisbetodylhunn
authored andcommitted
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
1 parent 8a9907c commit a40cd47

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Diff for: packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts

+5
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ function analyzeTestingModules(
437437

438438
for (const obj of testObjects) {
439439
const declarations = extractDeclarationsFromTestObject(obj, typeChecker);
440+
441+
if (declarations.length === 0) {
442+
continue;
443+
}
444+
440445
const importsProp = findLiteralProperty(obj, 'imports');
441446
const importElements = importsProp && hasNgModuleMetadataElements(importsProp) ?
442447
importsProp.initializer.elements :

Diff for: packages/core/schematics/test/standalone_migration_spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,34 @@ describe('standalone migration', () => {
10431043
`));
10441044
});
10451045

1046+
it('should not change testing objects with no declarations', async () => {
1047+
const initialContent = `
1048+
import {NgModule, Component} from '@angular/core';
1049+
import {TestBed} from '@angular/core/testing';
1050+
import {ButtonModule} from './button.module';
1051+
import {MatCardModule} from '@angular/material/card';
1052+
1053+
describe('bootrstrapping an app', () => {
1054+
it('should work', () => {
1055+
TestBed.configureTestingModule({
1056+
imports: [ButtonModule, MatCardModule]
1057+
});
1058+
const fixture = TestBed.createComponent(App);
1059+
expect(fixture.nativeElement.innerHTML).toBe('<hello>Hello</hello>');
1060+
});
1061+
});
1062+
1063+
@Component({template: 'hello'})
1064+
class App {}
1065+
`;
1066+
1067+
writeFile('app.spec.ts', initialContent);
1068+
1069+
await runMigration('convert-to-standalone');
1070+
1071+
expect(tree.readContent('app.spec.ts')).toBe(initialContent);
1072+
});
1073+
10461074
it('should migrate tests with a component declared through Catalyst', async () => {
10471075
writeFile('app.spec.ts', `
10481076
import {NgModule, Component} from '@angular/core';

0 commit comments

Comments
 (0)