feat(testing): add implicit test module
Every test now has an implicit module. It can be configured via `configureModule` (from @angular/core/testing)
to add providers, directives, pipes, ...
The compiler now has to be configured separately via `configureCompiler` (from @angular/core/testing)
to add providers or define whether to use jit.
BREAKING CHANGE:
- Application providers can no longer inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead. This reflects the
changes to `bootstrap` for module support (3f55aa609f
).
- Compiler providers can no longer be added via `addProviders` / `withProviders`.
Use the new method `configureCompiler` instead.
- Platform directives / pipes need to be provided via
`configureModule` and can no longer be provided via the
`PLATFORM_PIPES` / `PLATFORM_DIRECTIVES` tokens.
- `setBaseTestProviders()` was renamed into `initTestEnvironment` and
now takes a `PlatformRef` and a factory for a
`Compiler`.
- E.g. for the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {browserTestCompiler, browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
browserTestCompiler,
browserDynamicTestPlatform(),
BrowserDynamicTestModule);
```
- E.g. for the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {serverTestCompiler, serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
serverTestCompiler,
serverTestPlatform(),
ServerTestModule);
```
Related to #9726
Closes #9846
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {AppModuleFactory, AppModuleMetadata, Compiler, ComponentFactory, ComponentResolver, Injectable, Provider} from '@angular/core';
|
||||
import {AppModuleFactory, AppModuleMetadata, Compiler, ComponentFactory, ComponentResolver, ComponentStillLoadingError, Injectable, Injector, OptionalMetadata, Provider, SkipSelfMetadata} from '@angular/core';
|
||||
|
||||
import {BaseException} from '../src/facade/exceptions';
|
||||
import {ConcreteType, IS_DART, Type, isBlank, isString, stringify} from '../src/facade/lang';
|
||||
@ -43,11 +43,13 @@ export class RuntimeCompiler implements ComponentResolver, Compiler {
|
||||
private _compiledAppModuleCache = new Map<Type, AppModuleFactory<any>>();
|
||||
|
||||
constructor(
|
||||
private _metadataResolver: CompileMetadataResolver,
|
||||
private _injector: Injector, private _metadataResolver: CompileMetadataResolver,
|
||||
private _templateNormalizer: DirectiveNormalizer, private _templateParser: TemplateParser,
|
||||
private _styleCompiler: StyleCompiler, private _viewCompiler: ViewCompiler,
|
||||
private _appModuleCompiler: AppModuleCompiler, private _genConfig: CompilerConfig) {}
|
||||
|
||||
get injector(): Injector { return this._injector; }
|
||||
|
||||
resolveComponent(component: Type|string): Promise<ComponentFactory<any>> {
|
||||
if (isString(component)) {
|
||||
return PromiseWrapper.reject(
|
||||
@ -76,12 +78,15 @@ export class RuntimeCompiler implements ComponentResolver, Compiler {
|
||||
let componentCompilePromises: Promise<any>[] = [];
|
||||
if (!appModuleFactory || !useCache) {
|
||||
var compileModuleMeta = this._metadataResolver.getAppModuleMetadata(moduleType, metadata);
|
||||
let boundCompiler = new BoundCompiler(
|
||||
let boundCompilerFactory = (parentResolver: ComponentResolver) => new BoundCompiler(
|
||||
this, compileModuleMeta.directives.map(dir => dir.type.runtime),
|
||||
compileModuleMeta.pipes.map((pipe) => pipe.type.runtime));
|
||||
// Always provide a bound Compiler / ComponentResolver
|
||||
compileModuleMeta.providers.push(this._metadataResolver.getProviderMetadata(
|
||||
new Provider(Compiler, {useValue: boundCompiler})));
|
||||
compileModuleMeta.pipes.map((pipe) => pipe.type.runtime), parentResolver);
|
||||
// Always provide a bound Compiler and ComponentResolver
|
||||
compileModuleMeta.providers.push(
|
||||
this._metadataResolver.getProviderMetadata(new Provider(Compiler, {
|
||||
useFactory: boundCompilerFactory,
|
||||
deps: [[new OptionalMetadata(), new SkipSelfMetadata(), ComponentResolver]]
|
||||
})));
|
||||
compileModuleMeta.providers.push(this._metadataResolver.getProviderMetadata(
|
||||
new Provider(ComponentResolver, {useExisting: Compiler})));
|
||||
var compileResult = this._appModuleCompiler.compile(compileModuleMeta);
|
||||
@ -130,8 +135,7 @@ export class RuntimeCompiler implements ComponentResolver, Compiler {
|
||||
templates.forEach((template) => {
|
||||
if (template.loading) {
|
||||
if (isSync) {
|
||||
throw new BaseException(
|
||||
`Can't compile synchronously as ${template.compType.name} is still being loaded!`);
|
||||
throw new ComponentStillLoadingError(template.compType.runtime);
|
||||
} else {
|
||||
loadingPromises.push(template.loading);
|
||||
}
|
||||
@ -355,12 +359,19 @@ function assertComponent(meta: CompileDirectiveMetadata) {
|
||||
*/
|
||||
class BoundCompiler implements Compiler, ComponentResolver {
|
||||
constructor(
|
||||
private _delegate: RuntimeCompiler, private _directives: any[], private _pipes: any[]) {}
|
||||
private _delegate: RuntimeCompiler, private _directives: any[], private _pipes: any[],
|
||||
private _parentComponentResolver: ComponentResolver) {}
|
||||
|
||||
get injector(): Injector { return this._delegate.injector; }
|
||||
|
||||
resolveComponent(component: Type|string): Promise<ComponentFactory<any>> {
|
||||
if (isString(component)) {
|
||||
return PromiseWrapper.reject(
|
||||
new BaseException(`Cannot resolve component using '${component}'.`), null);
|
||||
if (this._parentComponentResolver) {
|
||||
return this._parentComponentResolver.resolveComponent(component);
|
||||
} else {
|
||||
return PromiseWrapper.reject(
|
||||
new BaseException(`Cannot resolve component using '${component}'.`), null);
|
||||
}
|
||||
}
|
||||
return this.compileComponentAsync(<ConcreteType<any>>component);
|
||||
}
|
||||
@ -388,7 +399,12 @@ class BoundCompiler implements Compiler, ComponentResolver {
|
||||
/**
|
||||
* Clears all caches
|
||||
*/
|
||||
clearCache(): void { this._delegate.clearCache(); }
|
||||
clearCache(): void {
|
||||
this._delegate.clearCache();
|
||||
if (this._parentComponentResolver) {
|
||||
this._parentComponentResolver.clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the cache for the given component/appModule.
|
||||
|
@ -12,18 +12,19 @@ import {DirectiveNormalizer} from '@angular/compiler/src/directive_normalizer';
|
||||
import {XHR} from '@angular/compiler/src/xhr';
|
||||
import {MockXHR} from '@angular/compiler/testing/xhr_mock';
|
||||
import {ViewEncapsulation} from '@angular/core/src/metadata/view';
|
||||
import {configureCompiler} from '@angular/core/testing';
|
||||
import {beforeEach, beforeEachProviders, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {SpyXHR} from './spies';
|
||||
import {TEST_PROVIDERS} from './test_bindings';
|
||||
import {TEST_COMPILER_PROVIDERS} from './test_bindings';
|
||||
|
||||
export function main() {
|
||||
describe('DirectiveNormalizer', () => {
|
||||
var dirType: CompileTypeMetadata;
|
||||
var dirTypeWithHttpUrl: CompileTypeMetadata;
|
||||
|
||||
beforeEachProviders(() => TEST_PROVIDERS);
|
||||
beforeEach(() => { configureCompiler({providers: TEST_COMPILER_PROVIDERS}); });
|
||||
|
||||
beforeEach(() => {
|
||||
dirType = new CompileTypeMetadata({moduleUrl: 'package:some/module/a.js', name: 'SomeComp'});
|
||||
@ -179,7 +180,7 @@ export function main() {
|
||||
|
||||
describe('normalizeExternalStylesheets', () => {
|
||||
|
||||
beforeEachProviders(() => [{provide: XHR, useClass: SpyXHR}]);
|
||||
beforeEach(() => { configureCompiler({providers: [{provide: XHR, useClass: SpyXHR}]}); });
|
||||
|
||||
it('should load an external stylesheet',
|
||||
inject(
|
||||
|
@ -9,17 +9,18 @@
|
||||
import {CompilerConfig} from '@angular/compiler/src/config';
|
||||
import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, ChangeDetectionStrategy, Component, Directive, DoCheck, Injectable, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewEncapsulation} from '@angular/core';
|
||||
import {LIFECYCLE_HOOKS_VALUES} from '@angular/core/src/metadata/lifecycle_hooks';
|
||||
import {configureCompiler} from '@angular/core/testing';
|
||||
import {afterEach, beforeEach, beforeEachProviders, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {IS_DART, stringify} from '../src/facade/lang';
|
||||
import {CompileMetadataResolver} from '../src/metadata_resolver';
|
||||
|
||||
import {MalformedStylesComponent} from './metadata_resolver_fixture';
|
||||
import {TEST_PROVIDERS} from './test_bindings';
|
||||
import {TEST_COMPILER_PROVIDERS} from './test_bindings';
|
||||
|
||||
export function main() {
|
||||
describe('CompileMetadataResolver', () => {
|
||||
beforeEachProviders(() => TEST_PROVIDERS);
|
||||
beforeEach(() => { configureCompiler({providers: TEST_COMPILER_PROVIDERS}); });
|
||||
|
||||
describe('getMetadata', () => {
|
||||
it('should read metadata',
|
||||
@ -110,11 +111,15 @@ export function main() {
|
||||
}));
|
||||
|
||||
describe('platform directives', () => {
|
||||
beforeEachProviders(() => [{
|
||||
provide: CompilerConfig,
|
||||
useValue: new CompilerConfig(
|
||||
{genDebugInfo: true, platformDirectives: [ADirective]})
|
||||
}]);
|
||||
beforeEach(() => {
|
||||
configureCompiler({
|
||||
providers: [{
|
||||
provide: CompilerConfig,
|
||||
useValue:
|
||||
new CompilerConfig({genDebugInfo: true, platformDirectives: [ADirective]})
|
||||
}]
|
||||
});
|
||||
});
|
||||
|
||||
it('should include platform directives when available',
|
||||
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import {beforeEach, ddescribe, xdescribe, describe, expect, iit, inject, beforeEachProviders, it, xit,} from '@angular/core/testing/testing_internal';
|
||||
import {Injectable, Component, Input, ViewMetadata, Compiler, ComponentFactory, Injector, AppModule, AppModuleMetadata, AppModuleFactory} from '@angular/core';
|
||||
import {ConcreteType, stringify} from '../src/facade/lang';
|
||||
import {fakeAsync, tick, TestComponentBuilder, ComponentFixture} from '@angular/core/testing';
|
||||
import {fakeAsync, tick, TestComponentBuilder, ComponentFixture, configureCompiler} from '@angular/core/testing';
|
||||
import {XHR, ViewResolver} from '@angular/compiler';
|
||||
import {MockViewResolver} from '@angular/compiler/testing';
|
||||
|
||||
@ -31,7 +31,7 @@ export function main() {
|
||||
let viewResolver: MockViewResolver;
|
||||
let injector: Injector;
|
||||
|
||||
beforeEachProviders(() => [{provide: XHR, useValue: new SpyXHR()}]);
|
||||
beforeEach(() => { configureCompiler({providers: [{provide: XHR, useClass: SpyXHR}]}); });
|
||||
|
||||
beforeEach(inject(
|
||||
[Compiler, TestComponentBuilder, XHR, ViewResolver, Injector],
|
||||
|
@ -14,13 +14,14 @@ import {TEMPLATE_TRANSFORMS, TemplateParser, splitClasses} from '@angular/compil
|
||||
import {MockSchemaRegistry} from '@angular/compiler/testing';
|
||||
import {SecurityContext} from '@angular/core';
|
||||
import {Console} from '@angular/core/src/console';
|
||||
import {configureCompiler} from '@angular/core/testing';
|
||||
import {afterEach, beforeEach, beforeEachProviders, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Identifiers, identifierToken} from '../src/identifiers';
|
||||
import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../src/interpolation_config';
|
||||
|
||||
import {unparse} from './expression_parser/unparser';
|
||||
import {TEST_PROVIDERS} from './test_bindings';
|
||||
import {TEST_COMPILER_PROVIDERS} from './test_bindings';
|
||||
|
||||
var someModuleUrl = 'package:someModule';
|
||||
|
||||
@ -39,9 +40,9 @@ export function main() {
|
||||
var console: ArrayConsole;
|
||||
|
||||
function commonBeforeEach() {
|
||||
beforeEachProviders(() => {
|
||||
beforeEach(() => {
|
||||
console = new ArrayConsole();
|
||||
return [{provide: Console, useValue: console}];
|
||||
configureCompiler({providers: [{provide: Console, useValue: console}]});
|
||||
});
|
||||
beforeEach(inject([TemplateParser], (parser: TemplateParser) => {
|
||||
var component = CompileDirectiveMetadata.create({
|
||||
@ -67,10 +68,14 @@ export function main() {
|
||||
}
|
||||
|
||||
describe('TemplateParser template transform', () => {
|
||||
beforeEachProviders(() => [TEST_PROVIDERS, MOCK_SCHEMA_REGISTRY]);
|
||||
beforeEach(() => { configureCompiler({providers: TEST_COMPILER_PROVIDERS}); });
|
||||
|
||||
beforeEachProviders(
|
||||
() => [{provide: TEMPLATE_TRANSFORMS, useValue: new FooAstTransformer(), multi: true}]);
|
||||
beforeEach(() => {
|
||||
configureCompiler({
|
||||
providers:
|
||||
[{provide: TEMPLATE_TRANSFORMS, useValue: new FooAstTransformer(), multi: true}]
|
||||
});
|
||||
});
|
||||
|
||||
describe('single', () => {
|
||||
commonBeforeEach();
|
||||
@ -80,8 +85,12 @@ export function main() {
|
||||
});
|
||||
|
||||
describe('multiple', () => {
|
||||
beforeEachProviders(
|
||||
() => [{provide: TEMPLATE_TRANSFORMS, useValue: new BarAstTransformer(), multi: true}]);
|
||||
beforeEach(() => {
|
||||
configureCompiler({
|
||||
providers:
|
||||
[{provide: TEMPLATE_TRANSFORMS, useValue: new BarAstTransformer(), multi: true}]
|
||||
});
|
||||
});
|
||||
|
||||
commonBeforeEach();
|
||||
it('should compose transformers', () => {
|
||||
@ -93,9 +102,14 @@ export function main() {
|
||||
describe('TemplateParser Security', () => {
|
||||
// Semi-integration test to make sure TemplateParser properly sets the security context.
|
||||
// Uses the actual DomElementSchemaRegistry.
|
||||
beforeEachProviders(
|
||||
() =>
|
||||
[TEST_PROVIDERS, {provide: ElementSchemaRegistry, useClass: DomElementSchemaRegistry}]);
|
||||
beforeEach(() => {
|
||||
configureCompiler({
|
||||
providers: [
|
||||
TEST_COMPILER_PROVIDERS,
|
||||
{provide: ElementSchemaRegistry, useClass: DomElementSchemaRegistry}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
commonBeforeEach();
|
||||
|
||||
@ -125,7 +139,9 @@ export function main() {
|
||||
});
|
||||
|
||||
describe('TemplateParser', () => {
|
||||
beforeEachProviders(() => [TEST_PROVIDERS, MOCK_SCHEMA_REGISTRY]);
|
||||
beforeEach(() => {
|
||||
configureCompiler({providers: [TEST_COMPILER_PROVIDERS, MOCK_SCHEMA_REGISTRY]});
|
||||
});
|
||||
|
||||
commonBeforeEach();
|
||||
|
||||
|
@ -11,7 +11,7 @@ import {createUrlResolverWithoutPackagePrefix} from '@angular/compiler/src/url_r
|
||||
import {MockSchemaRegistry} from '@angular/compiler/testing';
|
||||
import {MockXHR} from '@angular/compiler/testing/xhr_mock';
|
||||
|
||||
export var TEST_PROVIDERS: any[] = [
|
||||
export var TEST_COMPILER_PROVIDERS: any[] = [
|
||||
{provide: ElementSchemaRegistry, useValue: new MockSchemaRegistry({}, {})},
|
||||
{provide: XHR, useClass: MockXHR},
|
||||
{provide: UrlResolver, useFactory: createUrlResolverWithoutPackagePrefix}
|
||||
|
@ -6,8 +6,8 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {AnimationEntryMetadata, Compiler, ComponentFactory, Injectable, Injector, NgZone, ViewMetadata} from '@angular/core';
|
||||
import {ComponentFixture, ComponentFixtureNoNgZone, TestComponentBuilder} from '@angular/core/testing';
|
||||
import {AnimationEntryMetadata, Compiler, ComponentFactory, Inject, Injectable, Injector, NgZone, ViewMetadata} from '@angular/core';
|
||||
import {ComponentFixture, ComponentFixtureNoNgZone, TestComponentBuilder, TestInjector} from '@angular/core/testing';
|
||||
|
||||
import {DirectiveResolver, ViewResolver} from '../index';
|
||||
import {MapWrapper} from '../src/facade/collection';
|
||||
@ -54,9 +54,7 @@ export class OverridingTestComponentBuilder extends TestComponentBuilder {
|
||||
/** @internal */
|
||||
_viewOverrides = new Map<Type, ViewMetadata>();
|
||||
|
||||
|
||||
|
||||
constructor(injector: Injector) { super(injector); }
|
||||
constructor(@Inject(TestInjector) injector: Injector) { super(injector); }
|
||||
|
||||
/** @internal */
|
||||
_clone(): OverridingTestComponentBuilder {
|
||||
|
Reference in New Issue
Block a user