Skip to content

Commit 202e9a5

Browse files
cexbrayatangular-robot[bot]
authored andcommitted
fix(@schematics/angular): remove compileComponents from component test schematic
`compileComponents` is not necessary when using the CLI (as the templates are inlined) and just adds boilerplate code. So we can remove it from the test schematic and make it independent from `async/await` (only place we would have it in the CLI generated code, and in most Angular apps).
1 parent ba4414b commit 202e9a5

File tree

13 files changed

+54
-105
lines changed

13 files changed

+54
-105
lines changed

‎packages/angular_devkit/build_angular/src/builders/karma/tests/options/assets_spec.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
2323
import { NgModule } from '@angular/core';
2424
import { HttpClientModule } from '@angular/common/http';
2525
import { AppComponent } from './app.component';
26-
26+
2727
@NgModule({
2828
declarations: [
2929
AppComponent
@@ -40,7 +40,7 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
4040
'./src/app/app.component.ts': `
4141
import { Component } from '@angular/core';
4242
import { HttpClient } from '@angular/common/http';
43-
43+
4444
@Component({
4545
selector: 'app-root',
4646
template: '<p *ngFor="let asset of assets">{{ asset.content }}</p>'
@@ -62,19 +62,13 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
6262
import { TestBed } from '@angular/core/testing';
6363
import { HttpClientModule } from '@angular/common/http';
6464
import { AppComponent } from './app.component';
65-
65+
6666
describe('AppComponent', () => {
67-
beforeEach(async () => {
68-
await TestBed.configureTestingModule({
69-
imports: [
70-
HttpClientModule
71-
],
72-
declarations: [
73-
AppComponent
74-
]
75-
}).compileComponents();
76-
});
77-
67+
beforeEach(() => TestBed.configureTestingModule({
68+
imports: [HttpClientModule],
69+
declarations: [AppComponent]
70+
}));
71+
7872
it('should create the app', () => {
7973
const fixture = TestBed.createComponent(AppComponent);
8074
const app = fixture.debugElement.componentInstance;

‎packages/angular_devkit/build_angular/src/builders/karma/tests/options/styles_spec.ts

+7-19
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,9 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
2929
import { AppComponent } from './app.component';
3030
3131
describe('AppComponent', () => {
32-
beforeEach(async () => {
33-
await TestBed.configureTestingModule({
34-
imports: [
35-
],
36-
declarations: [
37-
AppComponent
38-
]
39-
}).compileComponents();
40-
});
32+
beforeEach(() => TestBed.configureTestingModule({
33+
declarations: [AppComponent]
34+
}));
4135
4236
it('should not contain text that is hidden via css', () => {
4337
const fixture = TestBed.createComponent(AppComponent);
@@ -97,16 +91,10 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
9791
import { HttpClientModule } from '@angular/common/http';
9892
import { AppComponent } from './app.component';
9993
describe('AppComponent', () => {
100-
beforeEach(async () => {
101-
await TestBed.configureTestingModule({
102-
imports: [
103-
HttpClientModule
104-
],
105-
declarations: [
106-
AppComponent
107-
]
108-
}).compileComponents();
109-
});
94+
beforeEach(() => TestBed.configureTestingModule({
95+
imports: [HttpClientModule],
96+
declarations: [AppComponent]
97+
}));
11098
it('should create the app', () => {
11199
const fixture = TestBed.createComponent(AppComponent);
112100
const app = fixture.debugElement.componentInstance;

‎packages/angular_devkit/build_angular/src/builders/karma/tests/options/web-worker-tsconfig_spec.ts

+7-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
3030
}`,
3131
'src/app/app.worker.ts': `
3232
/// <reference lib="webworker" />
33-
33+
3434
const prefix: string = 'Data: ';
3535
addEventListener('message', ({ data }) => {
3636
postMessage(prefix + data);
@@ -52,13 +52,9 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
5252
import { AppComponent } from './app.component';
5353
5454
describe('AppComponent', () => {
55-
beforeEach(async () => {
56-
await TestBed.configureTestingModule({
57-
declarations: [
58-
AppComponent
59-
]
60-
}).compileComponents();
61-
});
55+
beforeEach(() => TestBed.configureTestingModule({
56+
declarations: [AppComponent]
57+
}));
6258
6359
it('worker should be defined', () => {
6460
const fixture = TestBed.createComponent(AppComponent);
@@ -82,13 +78,9 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
8278
import { AppComponent } from './app.component';
8379
8480
describe('AppComponent', () => {
85-
beforeEach(async () => {
86-
await TestBed.configureTestingModule({
87-
declarations: [
88-
AppComponent
89-
]
90-
}).compileComponents();
91-
});
81+
beforeEach(() => TestBed.configureTestingModule({
82+
declarations: [AppComponent]
83+
}));
9284
9385
it('worker should throw', () => {
9486
expect(() => TestBed.createComponent(AppComponent))

‎packages/angular_devkit/build_angular/test/hello-world-app/src/app/app.component.spec.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
import { TestBed } from '@angular/core/testing';
1010
import { AppComponent } from './app.component';
1111
describe('AppComponent', () => {
12-
beforeEach(async () => {
13-
await TestBed.configureTestingModule({
14-
declarations: [AppComponent],
15-
}).compileComponents();
16-
});
12+
beforeEach(() => TestBed.configureTestingModule({
13+
declarations: [AppComponent],
14+
}));
1715
it('should create the app', () => {
1816
const fixture = TestBed.createComponent(AppComponent);
1917
const app = fixture.debugElement.componentInstance;

‎packages/angular_devkit/build_angular/test/hello-world-lib/projects/lib/src/lib/lib.component.spec.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ describe('LibComponent', () => {
1414
let component: LibComponent;
1515
let fixture: ComponentFixture<LibComponent>;
1616

17-
beforeEach(async () => {
18-
await TestBed.configureTestingModule({
19-
declarations: [ LibComponent ]
20-
})
21-
.compileComponents();
22-
});
17+
beforeEach(() => TestBed.configureTestingModule({
18+
declarations: [LibComponent]
19+
}));
2320

2421
beforeEach(() => {
2522
fixture = TestBed.createComponent(LibComponent);

‎packages/angular_devkit/build_webpack/test/angular-app/src/app/app.component.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import { TestBed } from '@angular/core/testing';
1010
import { AppComponent } from './app.component';
1111
describe('AppComponent', () => {
12-
beforeEach(async () => {
13-
await TestBed.configureTestingModule({
12+
beforeEach(() => {
13+
TestBed.configureTestingModule({
1414
declarations: [AppComponent],
15-
}).compileComponents();
15+
});
1616
});
1717
it('should create the app', () => {
1818
const fixture = TestBed.createComponent(AppComponent);

‎packages/schematics/angular/application/files/module-files/src/app/app.component.spec.ts.template

+4-10
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@ import { RouterTestingModule } from '@angular/router/testing';<% } %>
33
import { AppComponent } from './app.component';
44

55
describe('AppComponent', () => {
6-
beforeEach(async () => {
7-
await TestBed.configureTestingModule({<% if (routing) { %>
8-
imports: [
9-
RouterTestingModule
10-
],<% } %>
11-
declarations: [
12-
AppComponent
13-
],
14-
}).compileComponents();
15-
});
6+
beforeEach(() => TestBed.configureTestingModule({<% if (routing) { %>
7+
imports: [RouterTestingModule],<% } %>
8+
declarations: [AppComponent]
9+
}));
1610

1711
it('should create the app', () => {
1812
const fixture = TestBed.createComponent(AppComponent);

‎packages/schematics/angular/application/files/standalone-files/src/app/app.component.spec.ts.template

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import { TestBed } from '@angular/core/testing';
22
import { AppComponent } from './app.component';
33

44
describe('AppComponent', () => {
5-
beforeEach(async () => {
6-
await TestBed.configureTestingModule({
7-
imports: [AppComponent],
8-
}).compileComponents();
9-
});
5+
beforeEach(() => TestBed.configureTestingModule({
6+
imports: [AppComponent]
7+
}));
108

119
it('should create the app', () => {
1210
const fixture = TestBed.createComponent(AppComponent);

‎packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.spec.ts.template

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ describe('<%= classify(name) %><%= classify(type) %>', () => {
66
let component: <%= classify(name) %><%= classify(type) %>;
77
let fixture: ComponentFixture<<%= classify(name) %><%= classify(type) %>>;
88

9-
beforeEach(async () => {
10-
await TestBed.configureTestingModule({
11-
<%= standalone ? 'imports' : 'declarations' %>: [ <%= classify(name) %><%= classify(type) %> ]
12-
})
13-
.compileComponents();
14-
9+
beforeEach(() => {
10+
TestBed.configureTestingModule({
11+
<%= standalone ? 'imports' : 'declarations' %>: [<%= classify(name) %><%= classify(type) %>]
12+
});
1513
fixture = TestBed.createComponent(<%= classify(name) %><%= classify(type) %>);
1614
component = fixture.componentInstance;
1715
fixture.detectChanges();

‎packages/schematics/angular/component/index_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ describe('Component Schematic', () => {
431431
const options = { ...defaultOptions, standalone: true };
432432
const tree = await schematicRunner.runSchematic('component', options, appTree);
433433
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
434-
expect(testContent).toContain('imports: [ FooComponent ]');
434+
expect(testContent).toContain('imports: [FooComponent]');
435435
expect(testContent).not.toContain('declarations');
436436
});
437437
});

‎tests/legacy-cli/e2e/assets/13.0-project/src/app/app.component.spec.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@ import { RouterTestingModule } from '@angular/router/testing';
33
import { AppComponent } from './app.component';
44

55
describe('AppComponent', () => {
6-
beforeEach(async () => {
7-
await TestBed.configureTestingModule({
8-
imports: [
9-
RouterTestingModule
10-
],
11-
declarations: [
12-
AppComponent
13-
],
14-
}).compileComponents();
15-
});
6+
beforeEach(() => TestBed.configureTestingModule({
7+
imports: [RouterTestingModule],
8+
declarations: [AppComponent]
9+
}));
1610

1711
it('should create the app', () => {
1812
const fixture = TestBed.createComponent(AppComponent);

‎tests/legacy-cli/e2e/tests/test/test-jasmine-clock.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ export default async function () {
1818
jasmine.clock().uninstall();
1919
});
2020
21-
beforeEach(async () => {
22-
await TestBed.configureTestingModule({
23-
imports: [RouterTestingModule],
24-
declarations: [AppComponent],
25-
}).compileComponents();
26-
});
21+
beforeEach(() => TestBed.configureTestingModule({
22+
imports: [RouterTestingModule],
23+
declarations: [AppComponent],
24+
}));
2725
2826
it('should create the app', () => {
2927
const fixture = TestBed.createComponent(AppComponent);

‎tests/legacy-cli/e2e/tests/test/test-scripts.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ export default async function () {
3030
import { AppComponent } from './app.component';
3131
3232
describe('AppComponent', () => {
33-
beforeEach(async () => {
34-
await TestBed.configureTestingModule({
35-
declarations: [ AppComponent ]
36-
}).compileComponents();
37-
});
33+
beforeEach(() => TestBed.configureTestingModule({
34+
declarations: [AppComponent]
35+
}));
3836
3937
it('should have access to string-script.js', () => {
4038
let app = TestBed.createComponent(AppComponent).debugElement.componentInstance;

0 commit comments

Comments
 (0)