Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): re-add TestBed compileComponents …
Browse files Browse the repository at this point in the history
…in schematics to support defer block testing

The defer block support introduces a new asynchronous form of the set class metadata Angular function. This form
is needed to allow for providing metadata for dynamically imported deferred components. The asynchronous compileComponents
call within TestBed is now used to initialize this metadata during unit tests. Unit tests that contain defer blocks
must use this call prior to executing a test to allow templates containing defer to properly render. Existing tests that
do not use the new defer block do not require modification unless the defer block is introduced into components used in
the unit test.
  • Loading branch information
clydin committed Oct 4, 2023
1 parent 83020fc commit 0c20cc4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
Expand Up @@ -3,10 +3,16 @@ import { RouterTestingModule } from '@angular/router/testing';<% } %>
import { AppComponent } from './app.component';

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

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

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

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

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

fixture = TestBed.createComponent(<%= classify(name) %><%= classify(type) %>);
component = fixture.componentInstance;
fixture.detectChanges();
Expand Down
8 changes: 8 additions & 0 deletions packages/schematics/angular/component/index_spec.ts
Expand Up @@ -55,6 +55,14 @@ describe('Component Schematic', () => {
appTree = await schematicRunner.runSchematic('application', appOptions, appTree);
});

it('should contain a TestBed compileComponents call', async () => {
const options = { ...defaultOptions };

const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
expect(tsContent).toContain('compileComponents()');
});

it('should set change detection to OnPush', async () => {
const options = { ...defaultOptions, changeDetection: 'OnPush' };

Expand Down

0 comments on commit 0c20cc4

Please sign in to comment.