fix(migrations): don't add ModuleWithProviders to standalone test components (#48987)

Fixes that we were copying all expressions in the `imports` array of the test to the `imports` of the component, including any potential `ModuleWithProviders`.

Fixes #48971.

PR Close #48987
This commit is contained in:
Kristiyan Kostadinov
2023-02-07 11:27:32 +01:00
committed by Pawel Kozlowski
parent 770191cf1f
commit 1afa6ed322
2 changed files with 43 additions and 1 deletions
@@ -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) {
@@ -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';