Skip to content

Commit

Permalink
fix(migrations): avoid modifying testing modules without declarations (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
crisbeto authored and dylhunn committed Feb 2, 2023
1 parent 8a9907c commit a40cd47
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
28 changes: 28 additions & 0 deletions packages/core/schematics/test/standalone_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit a40cd47

Please sign in to comment.