8000 refactor(core): only throw an error if component with async metadata … · angular/angular@45ba049 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45ba049

Browse files
committed
refactor(core): only throw an error if component with async metadata is overriden.
This only really matters for tests that run with AOT.
1 parent 66753dc commit 45ba049

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

packages/core/test/test_bed_spec.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,87 @@ describe('TestBed', () => {
16571657
return ComponentClass;
16581658
};
16591659

1660+
it('should not require compileComponent if the component is not overridden', async () => {
1661+
const RootAotComponent = getAOTCompiledComponent('root', [], []);
1662+
1663+
TestBed.configureTestingModule({imports: [RootAotComponent]});
1664+
1665+
// If we had overriden the component, we would need to compile it
1666+
// but since we didn't, we can create the component synchronously
1667+
const fixture = TestBed.createComponent(RootAotComponent);
1668+
fixture.detectChanges();
1669+
1670+
expect(fixture.nativeElement.textContent).toBe('root cmp!');
1671+
});
1672+
1673+
it('should throw an error if a deferred component is not compiled', () => {
1674+
@Component({
1675+
selector: 'cmp-a',
1676+
template: 'CmpA!',
1677+
})
1678+
class CmpA {}
1679+
1680+
const NestedAotComponent = getAOTCompiledComponent('nested-cmp', [], [CmpA]);
1681+
const RootAotComponent = getAOTCompiledComponent('root', [], [NestedAotComponent]);
1682+
1683+
TestBed.configureTestingModule({imports: [RootAotComponent]});
1684+
1685+
TestBed.overrideComponent(RootAotComponent, {
1686+
set: {template: `Override of a root template! <nested-cmp />`},
1687+
});
1688+
TestBed.overrideComponent(NestedAotComponent, {
1689+
set: {template: `Override of a nested template! <cmp-a />`},
1690+
});
1691+
1692+
// We did override but not compile, therefore we expect to throw on createComponent
1693+
expect(() => TestBed.createComponent(RootAotComponent)).toThrowError(
1694+
`Component 'ComponentClass' has unresolved metadata. Please call \`await TestBed.compileComponents()\` before running this test.`,
1695+
);
1696+
});
1697+
1698+
it('should not throw if component is created after override+reset', async () => {
1699+
@Component({
1700+
selector: 'cmp-a',
1701+
template: 'CmpA!',
1702+
})
1703+
class CmpA {}
1704+
1705+
const NestedAotComponent = getAOTCompiledComponent('nested-cmp', [], [CmpA]);
1706+
const RootAotComponent = getAOTCompiledComponent('root', [], [NestedAotComponent]);
1707+
1708+
TestBed.configureTestingModule({imports: [RootAotComponent]});
1709+
1710+
TestBed.overrideComponent(RootAotComponent, {
1711+
set: {template: `Override of a root template! <nested-cmp />`},
1712+
});
1713+
TestBed.overrideComponent(NestedAotComponent, {
1714+
set: {template: `Override of a nested template! <cmp-a />`},
1715+
});
1716+
1717+
// Not compiled yet, so we expect to throw
1718+
expect(() => TestBed.createComponent(RootAotComponent)).toThrowError();
1719+
1720+
await TestBed.compileComponents();
1721+
1722+
// We're compiled now, so we can create the component
1723+
const fixture = TestBed.createComponent(RootAotComponent);
1724+
fixture.detectChanges();
1725+
1726+
expect(fixture.nativeElement.textContent).toBe(
1727+
'Override of a root template! Override of a nested template! CmpA!',
1728+
);
1729+
1730+
// We reset the override
1731+
TestBed.resetTestingModule();
1732+
TestBed.configureTestingModule({imports: [RootAotComponent]});
1733+
1734+
// We're back to the nominal behavior
1735+
const fixture2 = TestBed.createComponent(RootAotComponent);
1736+
fixture2.detectChanges();
1737+
1738+
expect(fixture2.nativeElement.textContent).toBe('root cmp!');
1739+
});
1740+
16601741
it('should handle async metadata on root and nested components', async () => {
16611742
@Component({
16621743
selector: 'cmp-a',

packages/core/testing/src/test_bed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ export class TestBedImpl implements TestBed {
625625< 629A /td>
const rootElId = `root${_nextRootElementId++}`;
626626
testComponentRenderer.insertRootElement(rootElId);
627627

628-
if (getAsyncClassMetadataFn(type)) {
628+
if (getAsyncClassMetadataFn(type) && this.compiler.hasPendingOverrides()) {
629629
throw new Error(
630630
`Component '${type.name}' has unresolved metadata. ` +
631631
`Please call \`await TestBed.compileComponents()\` before running this test.`,

packages/core/testing/src/test_bed_compiler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,14 @@ export class TestBedCompiler {
493493
}, [] as ComponentFactory<any>[]);
494494
}
495495

496+
hasPendingOverrides(): boolean {
497+
return (
498+
this.pendingComponents.size !== 0 ||
499+
this.pendingDirectives.size !== 0 ||
500+
this.pendingPipes.size !== 0
501+
);
502+
}
503+
496504
private compileTypesSync(): boolean {
497505
// Compile all queued components, directives, pipes.
498506
let needsAsyncResources = false;

0 commit comments

Comments
 (0)
0