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:
Tobias Bosch
2016-07-04 09:37:30 -07:00
parent 37e6da6dfb
commit 8d746e3f67
38 changed files with 1000 additions and 497 deletions

View File

@ -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(

View File

@ -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) => {

View File

@ -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],

View File

@ -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();

View File

@ -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}