feat: add .ngsummary.ts files to support AOT unit tests

Design doc: https://docs.google.com/document/d/1VmTkz0EbEVSWfEEWEvQ5sXyQXSCvtMOw4t7pKU-jOwc/edit?usp=sharing
This commit is contained in:
Tobias Bosch
2017-04-26 09:24:42 -07:00
committed by Matias Niemelä
parent 2714644528
commit 547c363473
28 changed files with 1187 additions and 197 deletions

View File

@ -6,7 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {CompilerOptions, Component, Directive, InjectionToken, Injector, ModuleWithComponentFactories, NgModule, NgModuleRef, NgZone, Pipe, PlatformRef, Provider, ReflectiveInjector, SchemaMetadata, Type, ɵERROR_COMPONENT_TYPE, ɵstringify as stringify} from '@angular/core';
import {CompilerOptions, Component, Directive, InjectionToken, Injector, ModuleWithComponentFactories, NgModule, NgModuleFactory, NgModuleRef, NgZone, Pipe, PlatformRef, Provider, ReflectiveInjector, SchemaMetadata, Type, ɵERROR_COMPONENT_TYPE, ɵstringify as stringify} from '@angular/core';
import {AsyncTestCompleter} from './async_test_completer';
import {ComponentFixture} from './component_fixture';
import {MetadataOverride} from './metadata_override';
@ -69,9 +70,10 @@ export class TestBed implements Injector {
*
* @experimental
*/
static initTestEnvironment(ngModule: Type<any>|Type<any>[], platform: PlatformRef): TestBed {
static initTestEnvironment(
ngModule: Type<any>|Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed {
const testBed = getTestBed();
testBed.initTestEnvironment(ngModule, platform);
testBed.initTestEnvironment(ngModule, platform, aotSummaries);
return testBed;
}
@ -151,7 +153,7 @@ export class TestBed implements Injector {
private _compiler: TestingCompiler = null !;
private _moduleRef: NgModuleRef<any> = null !;
private _moduleWithComponentFactories: ModuleWithComponentFactories<any> = null !;
private _moduleFactory: NgModuleFactory<any> = null !;
private _compilerOptions: CompilerOptions[] = [];
@ -166,6 +168,12 @@ export class TestBed implements Injector {
private _schemas: Array<SchemaMetadata|any[]> = [];
private _activeFixtures: ComponentFixture<any>[] = [];
private _aotSummaries: () => any[] = () => [];
platform: PlatformRef = null !;
ngModule: Type<any>|Type<any>[] = null !;
/**
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
* angular module. These are common to every test in the suite.
@ -179,12 +187,16 @@ export class TestBed implements Injector {
*
* @experimental
*/
initTestEnvironment(ngModule: Type<any>|Type<any>[], platform: PlatformRef) {
initTestEnvironment(
ngModule: Type<any>|Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]) {
if (this.platform || this.ngModule) {
throw new Error('Cannot set base providers because it has already been called');
}
this.platform = platform;
this.ngModule = ngModule;
if (aotSummaries) {
this._aotSummaries = aotSummaries;
}
}
/**
@ -196,6 +208,7 @@ export class TestBed implements Injector {
this.resetTestingModule();
this.platform = null !;
this.ngModule = null !;
this._aotSummaries = () => [];
}
resetTestingModule() {
@ -206,7 +219,7 @@ export class TestBed implements Injector {
this._pipeOverrides = [];
this._moduleRef = null !;
this._moduleWithComponentFactories = null !;
this._moduleFactory = null !;
this._compilerOptions = [];
this._providers = [];
this._declarations = [];
@ -223,10 +236,6 @@ export class TestBed implements Injector {
this._activeFixtures = [];
}
platform: PlatformRef = null !;
ngModule: Type<any>|Type<any>[] = null !;
configureCompiler(config: {providers?: any[], useJit?: boolean}) {
this._assertNotInstantiated('TestBed.configureCompiler', 'configure the compiler');
this._compilerOptions.push(config);
@ -249,14 +258,14 @@ export class TestBed implements Injector {
}
compileComponents(): Promise<any> {
if (this._moduleWithComponentFactories || this._instantiated) {
if (this._moduleFactory || this._instantiated) {
return Promise.resolve(null);
}
const moduleType = this._createCompilerAndModule();
return this._compiler.compileModuleAndAllComponentsAsync(moduleType)
.then((moduleAndComponentFactories) => {
this._moduleWithComponentFactories = moduleAndComponentFactories;
this._moduleFactory = moduleAndComponentFactories.ngModuleFactory;
});
}
@ -264,11 +273,11 @@ export class TestBed implements Injector {
if (this._instantiated) {
return;
}
if (!this._moduleWithComponentFactories) {
if (!this._moduleFactory) {
try {
const moduleType = this._createCompilerAndModule();
this._moduleWithComponentFactories =
this._compiler.compileModuleAndAllComponentsSync(moduleType);
this._moduleFactory =
this._compiler.compileModuleAndAllComponentsSync(moduleType).ngModuleFactory;
} catch (e) {
if (getComponentType(e)) {
throw new Error(
@ -282,7 +291,7 @@ export class TestBed implements Injector {
const ngZone = new NgZone({enableLongStackTrace: true});
const ngZoneInjector = ReflectiveInjector.resolveAndCreate(
[{provide: NgZone, useValue: ngZone}], this.platform.injector);
this._moduleRef = this._moduleWithComponentFactories.ngModuleFactory.create(ngZoneInjector);
this._moduleRef = this._moduleFactory.create(ngZoneInjector);
this._instantiated = true;
}
@ -300,6 +309,7 @@ export class TestBed implements Injector {
this.platform.injector.get(TestingCompilerFactory);
this._compiler =
compilerFactory.createTestingCompiler(this._compilerOptions.concat([{useDebug: true}]));
this._compiler.loadAotSummaries(this._aotSummaries);
this._moduleOverrides.forEach((entry) => this._compiler.overrideModule(entry[0], entry[1]));
this._componentOverrides.forEach(
(entry) => this._compiler.overrideComponent(entry[0], entry[1]));
@ -356,8 +366,7 @@ export class TestBed implements Injector {
createComponent<T>(component: Type<T>): ComponentFixture<T> {
this._initIfNeeded();
const componentFactory = this._moduleWithComponentFactories.componentFactories.find(
(compFactory) => compFactory.componentType === component);
const componentFactory = this._compiler.getComponentFactory(component);
if (!componentFactory) {
throw new Error(

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Compiler, CompilerOptions, Component, Directive, Injector, NgModule, Pipe, Type} from '@angular/core';
import {Compiler, CompilerOptions, Component, ComponentFactory, Directive, Injector, NgModule, Pipe, Type} from '@angular/core';
import {MetadataOverride} from './metadata_override';
@ -33,6 +33,18 @@ export class TestingCompiler extends Compiler {
overridePipe(directive: Type<any>, overrides: MetadataOverride<Pipe>): void {
throw unimplemented();
}
/**
* Allows to pass the compile summary from AOT compilation to the JIT compiler,
* so that it can use the code generated by AOT.
*/
loadAotSummaries(summaries: () => any[]) { throw unimplemented(); };
/**
* Gets the component factory for the given component.
* This assumes that the component has been compiled before calling this call using
* `compileModuleAndAllComponents*`.
*/
getComponentFactory<T>(component: Type<T>): ComponentFactory<T> { throw unimplemented(); }
}
/**