mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
committed by
Pawel Kozlowski
parent
770191cf1f
commit
1afa6ed322
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user