Skip to content

Commit

Permalink
fix(@schematics/angular): remove compileComponents from component tes…
Browse files Browse the repository at this point in the history
…t 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).
  • Loading branch information
cexbrayat authored and angular-robot[bot] committed Mar 24, 2023
1 parent ba4414b commit 202e9a5
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
Expand All @@ -40,7 +40,7 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
'./src/app/app.component.ts': `
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
template: '<p *ngFor="let asset of assets">{{ asset.content }}</p>'
Expand All @@ -62,19 +62,13 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
import { TestBed } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
HttpClientModule
],
declarations: [
AppComponent
]
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
imports: [HttpClientModule],
declarations: [AppComponent]
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
],
declarations: [
AppComponent
]
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
declarations: [AppComponent]
}));
it('should not contain text that is hidden via css', () => {
const fixture = TestBed.createComponent(AppComponent);
Expand Down Expand Up @@ -97,16 +91,10 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
HttpClientModule
],
declarations: [
AppComponent
]
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
imports: [HttpClientModule],
declarations: [AppComponent]
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
}`,
'src/app/app.worker.ts': `
/// <reference lib="webworker" />
const prefix: string = 'Data: ';
addEventListener('message', ({ data }) => {
postMessage(prefix + data);
Expand All @@ -52,13 +52,9 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
AppComponent
]
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
declarations: [AppComponent]
}));
it('worker should be defined', () => {
const fixture = TestBed.createComponent(AppComponent);
Expand All @@ -82,13 +78,9 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
AppComponent
]
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
declarations: [AppComponent]
}));
it('worker should throw', () => {
expect(() => TestBed.createComponent(AppComponent))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [AppComponent],
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
declarations: [AppComponent],
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ describe('LibComponent', () => {
let component: LibComponent;
let fixture: ComponentFixture<LibComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ LibComponent ]
})
.compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
declarations: [LibComponent]
}));

beforeEach(() => {
fixture = TestBed.createComponent(LibComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
}).compileComponents();
});
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ import { RouterTestingModule } from '@angular/router/testing';<% } %>
import { AppComponent } from './app.component';

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({<% if (routing) { %>
imports: [
RouterTestingModule
],<% } %>
declarations: [
AppComponent
],
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({<% if (routing) { %>
imports: [RouterTestingModule],<% } %>
declarations: [AppComponent]
}));

it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppComponent],
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
imports: [AppComponent]
}));

it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ describe('<%= classify(name) %><%= classify(type) %>', () => {
let component: <%= classify(name) %><%= classify(type) %>;
let fixture: ComponentFixture<<%= classify(name) %><%= classify(type) %>>;

beforeEach(async () => {
await TestBed.configureTestingModule({
<%= standalone ? 'imports' : 'declarations' %>: [ <%= classify(name) %><%= classify(type) %> ]
})
.compileComponents();

beforeEach(() => {
TestBed.configureTestingModule({
<%= standalone ? 'imports' : 'declarations' %>: [<%= classify(name) %><%= classify(type) %>]
});
fixture = TestBed.createComponent(<%= classify(name) %><%= classify(type) %>);
component = fixture.componentInstance;
fixture.detectChanges();
Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/angular/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ describe('Component Schematic', () => {
const options = { ...defaultOptions, standalone: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
expect(testContent).toContain('imports: [ FooComponent ]');
expect(testContent).toContain('imports: [FooComponent]');
expect(testContent).not.toContain('declarations');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
AppComponent
],
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [AppComponent]
}));

it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
Expand Down
10 changes: 4 additions & 6 deletions tests/legacy-cli/e2e/tests/test/test-jasmine-clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ export default async function () {
jasmine.clock().uninstall();
});
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [AppComponent],
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [AppComponent],
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
Expand Down
8 changes: 3 additions & 5 deletions tests/legacy-cli/e2e/tests/test/test-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ export default async function () {
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ AppComponent ]
}).compileComponents();
});
beforeEach(() => TestBed.configureTestingModule({
declarations: [AppComponent]
}));
it('should have access to string-script.js', () => {
let app = TestBed.createComponent(AppComponent).debugElement.componentInstance;
Expand Down

0 comments on commit 202e9a5

Please sign in to comment.