I have an Angular (4.x) application with a top-level module AppModule
, which declares several custom components, so they can be used in templates.
However, for jasmine/karma tests the most common approach seems to be using BrowserDynamicTestingModule
and declare any required custom components during beforeEach
, e.g.:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent, SomeDependedUponComponent ]
})
.compileComponents();
}));
Considering we're simulating the main application's environment anyway, why wouldn't one just initialize the testBed with AppModule
, so every custom component is available to all tests? Something like:
getTestBed().initTestEnvironment(
[BrowserDynamicTestingModule, AppModule],
platformBrowserDynamicTesting()
);
If I'm not missing something, this is much closer to the main application's setup and even reduces boilerplate code in individual spec/test files. Orre there any disadvantages, such as a performance penalty?