Skip to content

Commit 1afa6ed

Browse files
crisbetopkozlowski-opensource
authored andcommitted
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
1 parent 770191c commit 1afa6ed

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,8 @@ function analyzeTestingModules(
569569

570570
const importsProp = findLiteralProperty(obj, 'imports');
571571
const importElements = importsProp && hasNgModuleMetadataElements(importsProp) ?
572-
importsProp.initializer.elements :
572+
// Filter out calls since they may be a `ModuleWithProviders`.
573+
importsProp.initializer.elements.filter(el => !ts.isCallExpression(el)) :
573574
null;
574575

575576
for (const decl of declarations) {

packages/core/schematics/test/standalone_migration_spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,47 @@ describe('standalone migration', () => {
10951095
`));
10961096
});
10971097

1098+
it('should not add ModuleWithProviders imports to the `imports` in a test', async () => {
1099+
writeFile('app.spec.ts', `
1100+
import {NgModule, Component} from '@angular/core';
1101+
import {TestBed} from '@angular/core/testing';
1102+
import {MatCardModule} from '@angular/material/card';
1103+
1104+
describe('bootrstrapping an app', () => {
1105+
it('should work', () => {
1106+
TestBed.configureTestingModule({
1107+
declarations: [App],
1108+
imports: [MatCardModule.forRoot({})]
1109+
});
1110+
const fixture = TestBed.createComponent(App);
1111+
expect(fixture.nativeElement.innerHTML).toBe('hello');
1112+
});
1113+
});
1114+
1115+
@Component({template: 'hello'})
1116+
class App {}
1117+
`);
1118+
1119+
await runMigration('convert-to-standalone');
1120+
1121+
const content = stripWhitespace(tree.readContent('app.spec.ts'));
1122+
1123+
expect(content).toContain(stripWhitespace(`
1124+
@Component({template: 'hello', standalone: true})
1125+
class App {}
1126+
`));
1127+
1128+
expect(content).toContain(stripWhitespace(`
1129+
it('should work', () => {
1130+
TestBed.configureTestingModule({
1131+
imports: [MatCardModule.forRoot({}), App]
1132+
});
1133+
const fixture = TestBed.createComponent(App);
1134+
expect(fixture.nativeElement.innerHTML).toBe('hello');
1135+
});
1136+
`));
1137+
});
1138+
10981139
it('should not change testing objects with no declarations', async () => {
10991140
const initialContent = `
11001141
import {NgModule, Component} from '@angular/core';

0 commit comments

Comments
 (0)