Skip to content

Instantly share code, notes, and snippets.

@topnotch48
Last active April 22, 2018 14:13
Show Gist options
  • Save topnotch48/077b043213bf602baba45a6268ae8a5a to your computer and use it in GitHub Desktop.
Save topnotch48/077b043213bf602baba45a6268ae8a5a to your computer and use it in GitHub Desktop.
private _initIfNeeded() {
if (this._instantiated) {
return;
}
if (!this._moduleFactory) {
try {
const moduleType = this._createCompilerAndModule();
this._moduleFactory =
this._compiler.compileModuleAndAllComponentsSync(moduleType).ngModuleFactory;
} catch (e) {
const errorCompType = this._compiler.getComponentFromError(e);
if (errorCompType) {
throw new Error(
`This test module uses the component ${stringify(errorCompType)} which is using a "templateUrl" or "styleUrls", but they were never compiled. ` +
`Please call "TestBed.compileComponents" before your test.`);
} else {
throw e;
}
}
}
for (const {component, templateOf} of this._templateOverrides) {
const compFactory = this._compiler.getComponentFactory(templateOf);
overrideComponentView(component, compFactory);
}
const ngZone = new NgZone({enableLongStackTrace: true});
const providers: StaticProvider[] = [{provide: NgZone,
useValue: ngZone}];
const ngZoneInjector = Injector.create({
providers: providers,
parent: this.platform.injector,
name: this._moduleFactory.moduleType.name
});
this._moduleRef = this._moduleFactory.create(ngZoneInjector);
(this._moduleRef.injector.get(ApplicationInitStatus) as any).runInitializers();
this._instantiated = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment