2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 12:33:29 -07:00
|
|
|
import {ApplicationInitStatus, CompilerOptions, Component, Directive, InjectionToken, Injector, ModuleWithComponentFactories, NgModule, NgModuleFactory, NgModuleRef, NgZone, Optional, Pipe, PlatformRef, Provider, SchemaMetadata, SkipSelf, Type, ɵDepFlags as DepFlags, ɵERROR_COMPONENT_TYPE, ɵNodeFlags as NodeFlags, ɵclearProviderOverrides as clearProviderOverrides, ɵoverrideProvider as overrideProvider, ɵstringify as stringify} from '@angular/core';
|
2017-04-26 09:24:42 -07:00
|
|
|
|
2016-04-26 13:06:50 -07:00
|
|
|
import {AsyncTestCompleter} from './async_test_completer';
|
2016-07-28 04:54:49 -07:00
|
|
|
import {ComponentFixture} from './component_fixture';
|
|
|
|
import {MetadataOverride} from './metadata_override';
|
|
|
|
import {TestingCompiler, TestingCompilerFactory} from './test_compiler';
|
2016-04-26 13:06:50 -07:00
|
|
|
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
const UNDEFINED = new Object();
|
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
/**
|
|
|
|
* An abstract class for inserting the root test component element in a platform independent way.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export class TestComponentRenderer {
|
|
|
|
insertRootElement(rootElementId: string) {}
|
|
|
|
}
|
|
|
|
|
2016-09-16 10:57:57 -07:00
|
|
|
let _nextRootElementId = 0;
|
2016-07-28 04:54:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-01-03 16:54:46 -08:00
|
|
|
export const ComponentFixtureAutoDetect =
|
|
|
|
new InjectionToken<boolean[]>('ComponentFixtureAutoDetect');
|
2016-07-28 04:54:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-01-03 16:54:46 -08:00
|
|
|
export const ComponentFixtureNoNgZone = new InjectionToken<boolean[]>('ComponentFixtureNoNgZone');
|
2016-07-28 04:54:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export type TestModuleMetadata = {
|
2016-09-16 10:57:57 -07:00
|
|
|
providers?: any[],
|
|
|
|
declarations?: any[],
|
|
|
|
imports?: any[],
|
|
|
|
schemas?: Array<SchemaMetadata|any[]>,
|
2016-08-30 18:07:40 -07:00
|
|
|
};
|
2016-07-28 04:54:49 -07:00
|
|
|
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
/**
|
2016-09-22 10:32:17 -07:00
|
|
|
* @whatItDoes Configures and initializes environment for unit testing and provides methods for
|
|
|
|
* creating components and services in unit tests.
|
|
|
|
* @description
|
|
|
|
*
|
|
|
|
* TestBed is the primary api for writing unit tests for Angular applications and libraries.
|
|
|
|
*
|
|
|
|
* @stable
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
*/
|
2016-07-20 10:51:21 -07:00
|
|
|
export class TestBed implements Injector {
|
2016-07-28 04:54:49 -07:00
|
|
|
/**
|
|
|
|
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
|
|
|
|
* angular module. These are common to every test in the suite.
|
|
|
|
*
|
|
|
|
* This may only be called once, to set up the common providers for the current test
|
|
|
|
* suite on the current platform. If you absolutely need to change the providers,
|
|
|
|
* first use `resetTestEnvironment`.
|
|
|
|
*
|
|
|
|
* Test modules and platforms for individual platforms are available from
|
2016-09-10 01:36:38 +09:00
|
|
|
* '@angular/<platform_name>/testing'.
|
2016-07-28 04:54:49 -07:00
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-04-26 09:24:42 -07:00
|
|
|
static initTestEnvironment(
|
|
|
|
ngModule: Type<any>|Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed {
|
2016-07-28 04:54:49 -07:00
|
|
|
const testBed = getTestBed();
|
2017-04-26 09:24:42 -07:00
|
|
|
testBed.initTestEnvironment(ngModule, platform, aotSummaries);
|
2016-07-28 04:54:49 -07:00
|
|
|
return testBed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the providers for the test injector.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
static resetTestEnvironment() { getTestBed().resetTestEnvironment(); }
|
|
|
|
|
|
|
|
static resetTestingModule(): typeof TestBed {
|
|
|
|
getTestBed().resetTestingModule();
|
|
|
|
return TestBed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows overriding default compiler providers and settings
|
|
|
|
* which are defined in test_injector.js
|
|
|
|
*/
|
|
|
|
static configureCompiler(config: {providers?: any[]; useJit?: boolean;}): typeof TestBed {
|
|
|
|
getTestBed().configureCompiler(config);
|
|
|
|
return TestBed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows overriding default providers, directives, pipes, modules of the test injector,
|
|
|
|
* which are defined in test_injector.js
|
|
|
|
*/
|
|
|
|
static configureTestingModule(moduleDef: TestModuleMetadata): typeof TestBed {
|
|
|
|
getTestBed().configureTestingModule(moduleDef);
|
|
|
|
return TestBed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile components with a `templateUrl` for the test's NgModule.
|
|
|
|
* It is necessary to call this function
|
|
|
|
* as fetching urls is asynchronous.
|
|
|
|
*/
|
|
|
|
static compileComponents(): Promise<any> { return getTestBed().compileComponents(); }
|
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): typeof TestBed {
|
2016-07-28 04:54:49 -07:00
|
|
|
getTestBed().overrideModule(ngModule, override);
|
|
|
|
return TestBed;
|
|
|
|
}
|
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
static overrideComponent(component: Type<any>, override: MetadataOverride<Component>):
|
2016-08-10 18:21:28 -07:00
|
|
|
typeof TestBed {
|
2016-07-28 04:54:49 -07:00
|
|
|
getTestBed().overrideComponent(component, override);
|
|
|
|
return TestBed;
|
|
|
|
}
|
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>):
|
2016-08-10 18:21:28 -07:00
|
|
|
typeof TestBed {
|
2016-07-28 04:54:49 -07:00
|
|
|
getTestBed().overrideDirective(directive, override);
|
|
|
|
return TestBed;
|
|
|
|
}
|
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): typeof TestBed {
|
2016-07-28 04:54:49 -07:00
|
|
|
getTestBed().overridePipe(pipe, override);
|
|
|
|
return TestBed;
|
|
|
|
}
|
|
|
|
|
2016-12-15 02:05:17 +03:00
|
|
|
static overrideTemplate(component: Type<any>, template: string): typeof TestBed {
|
2017-03-29 09:34:45 -07:00
|
|
|
getTestBed().overrideComponent(component, {set: {template, templateUrl: null !}});
|
2016-12-15 02:05:17 +03:00
|
|
|
return TestBed;
|
|
|
|
}
|
|
|
|
|
2017-05-15 13:12:10 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrites all providers for the given token with the given provider definition.
|
|
|
|
*/
|
|
|
|
static overrideProvider(token: any, provider: {
|
|
|
|
useFactory: Function,
|
|
|
|
deps: any[],
|
|
|
|
}): void;
|
|
|
|
static overrideProvider(token: any, provider: {useValue: any;}): void;
|
|
|
|
static overrideProvider(token: any, provider: {
|
|
|
|
useFactory?: Function,
|
|
|
|
useValue?: any,
|
|
|
|
deps?: any[],
|
|
|
|
}): typeof TestBed {
|
|
|
|
getTestBed().overrideProvider(token, provider as any);
|
|
|
|
return TestBed;
|
|
|
|
}
|
|
|
|
|
2016-08-16 20:20:48 -07:00
|
|
|
static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) {
|
|
|
|
return getTestBed().get(token, notFoundValue);
|
|
|
|
}
|
|
|
|
|
2016-08-10 18:21:28 -07:00
|
|
|
static createComponent<T>(component: Type<T>): ComponentFixture<T> {
|
2016-07-28 04:54:49 -07:00
|
|
|
return getTestBed().createComponent(component);
|
|
|
|
}
|
|
|
|
|
2015-12-08 19:03:21 -08:00
|
|
|
private _instantiated: boolean = false;
|
|
|
|
|
2017-03-29 09:34:45 -07:00
|
|
|
private _compiler: TestingCompiler = null !;
|
|
|
|
private _moduleRef: NgModuleRef<any> = null !;
|
2017-04-26 09:24:42 -07:00
|
|
|
private _moduleFactory: NgModuleFactory<any> = null !;
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
|
2016-07-18 03:50:31 -07:00
|
|
|
private _compilerOptions: CompilerOptions[] = [];
|
2015-12-08 19:03:21 -08:00
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
private _moduleOverrides: [Type<any>, MetadataOverride<NgModule>][] = [];
|
|
|
|
private _componentOverrides: [Type<any>, MetadataOverride<Component>][] = [];
|
|
|
|
private _directiveOverrides: [Type<any>, MetadataOverride<Directive>][] = [];
|
|
|
|
private _pipeOverrides: [Type<any>, MetadataOverride<Pipe>][] = [];
|
2016-07-28 04:54:49 -07:00
|
|
|
|
2016-08-15 19:37:42 -07:00
|
|
|
private _providers: Provider[] = [];
|
2016-08-10 18:21:28 -07:00
|
|
|
private _declarations: Array<Type<any>|any[]|any> = [];
|
|
|
|
private _imports: Array<Type<any>|any[]|any> = [];
|
2016-07-25 03:02:57 -07:00
|
|
|
private _schemas: Array<SchemaMetadata|any[]> = [];
|
2016-08-25 23:37:46 +02:00
|
|
|
private _activeFixtures: ComponentFixture<any>[] = [];
|
2015-12-08 19:03:21 -08:00
|
|
|
|
2017-04-26 09:24:42 -07:00
|
|
|
private _aotSummaries: () => any[] = () => [];
|
|
|
|
|
|
|
|
platform: PlatformRef = null !;
|
|
|
|
|
|
|
|
ngModule: Type<any>|Type<any>[] = null !;
|
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
/**
|
|
|
|
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
|
|
|
|
* angular module. These are common to every test in the suite.
|
|
|
|
*
|
|
|
|
* This may only be called once, to set up the common providers for the current test
|
|
|
|
* suite on the current platform. If you absolutely need to change the providers,
|
|
|
|
* first use `resetTestEnvironment`.
|
|
|
|
*
|
|
|
|
* Test modules and platforms for individual platforms are available from
|
2016-09-10 01:36:38 +09:00
|
|
|
* '@angular/<platform_name>/testing'.
|
2016-07-28 04:54:49 -07:00
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-04-26 09:24:42 -07:00
|
|
|
initTestEnvironment(
|
|
|
|
ngModule: Type<any>|Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]) {
|
2016-07-28 04:54:49 -07:00
|
|
|
if (this.platform || this.ngModule) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error('Cannot set base providers because it has already been called');
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
|
|
|
this.platform = platform;
|
|
|
|
this.ngModule = ngModule;
|
2017-04-26 09:24:42 -07:00
|
|
|
if (aotSummaries) {
|
|
|
|
this._aotSummaries = aotSummaries;
|
|
|
|
}
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the providers for the test injector.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
resetTestEnvironment() {
|
|
|
|
this.resetTestingModule();
|
2017-03-29 09:34:45 -07:00
|
|
|
this.platform = null !;
|
|
|
|
this.ngModule = null !;
|
2017-04-26 09:24:42 -07:00
|
|
|
this._aotSummaries = () => [];
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
resetTestingModule() {
|
2017-05-15 13:12:10 -07:00
|
|
|
clearProviderOverrides();
|
2017-03-29 09:34:45 -07:00
|
|
|
this._compiler = null !;
|
2016-07-28 04:54:49 -07:00
|
|
|
this._moduleOverrides = [];
|
|
|
|
this._componentOverrides = [];
|
|
|
|
this._directiveOverrides = [];
|
|
|
|
this._pipeOverrides = [];
|
|
|
|
|
2017-03-29 09:34:45 -07:00
|
|
|
this._moduleRef = null !;
|
2017-04-26 09:24:42 -07:00
|
|
|
this._moduleFactory = null !;
|
2016-07-18 03:50:31 -07:00
|
|
|
this._compilerOptions = [];
|
2015-12-08 19:03:21 -08:00
|
|
|
this._providers = [];
|
2016-07-18 03:50:31 -07:00
|
|
|
this._declarations = [];
|
|
|
|
this._imports = [];
|
2016-07-25 03:02:57 -07:00
|
|
|
this._schemas = [];
|
2015-12-08 19:03:21 -08:00
|
|
|
this._instantiated = false;
|
2017-03-14 14:32:26 -07:00
|
|
|
this._activeFixtures.forEach((fixture) => {
|
|
|
|
try {
|
|
|
|
fixture.destroy();
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Error during cleanup of component', fixture.componentInstance);
|
|
|
|
}
|
|
|
|
});
|
2016-08-25 23:37:46 +02:00
|
|
|
this._activeFixtures = [];
|
2015-12-08 19:03:21 -08:00
|
|
|
}
|
|
|
|
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
configureCompiler(config: {providers?: any[], useJit?: boolean}) {
|
2016-07-28 04:54:49 -07:00
|
|
|
this._assertNotInstantiated('TestBed.configureCompiler', 'configure the compiler');
|
2016-07-18 03:50:31 -07:00
|
|
|
this._compilerOptions.push(config);
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
}
|
2015-12-15 16:38:27 -08:00
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
configureTestingModule(moduleDef: TestModuleMetadata) {
|
|
|
|
this._assertNotInstantiated('TestBed.configureTestingModule', 'configure the test module');
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
if (moduleDef.providers) {
|
2016-09-16 10:57:57 -07:00
|
|
|
this._providers.push(...moduleDef.providers);
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
}
|
2016-07-18 03:50:31 -07:00
|
|
|
if (moduleDef.declarations) {
|
2016-09-16 10:57:57 -07:00
|
|
|
this._declarations.push(...moduleDef.declarations);
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
}
|
2016-07-18 03:50:31 -07:00
|
|
|
if (moduleDef.imports) {
|
2016-09-16 10:57:57 -07:00
|
|
|
this._imports.push(...moduleDef.imports);
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
}
|
2016-07-25 03:02:57 -07:00
|
|
|
if (moduleDef.schemas) {
|
2016-09-16 10:57:57 -07:00
|
|
|
this._schemas.push(...moduleDef.schemas);
|
2016-07-25 03:02:57 -07:00
|
|
|
}
|
2015-12-08 19:03:21 -08:00
|
|
|
}
|
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
compileComponents(): Promise<any> {
|
2017-04-26 09:24:42 -07:00
|
|
|
if (this._moduleFactory || this._instantiated) {
|
2016-07-28 04:54:49 -07:00
|
|
|
return Promise.resolve(null);
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
}
|
2016-07-20 10:51:21 -07:00
|
|
|
|
2016-07-18 03:50:31 -07:00
|
|
|
const moduleType = this._createCompilerAndModule();
|
2016-07-28 04:54:49 -07:00
|
|
|
return this._compiler.compileModuleAndAllComponentsAsync(moduleType)
|
|
|
|
.then((moduleAndComponentFactories) => {
|
2017-04-26 09:24:42 -07:00
|
|
|
this._moduleFactory = moduleAndComponentFactories.ngModuleFactory;
|
2016-07-28 04:54:49 -07:00
|
|
|
});
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
}
|
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
private _initIfNeeded() {
|
2016-07-20 10:51:21 -07:00
|
|
|
if (this._instantiated) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-26 09:24:42 -07:00
|
|
|
if (!this._moduleFactory) {
|
2016-07-28 04:54:49 -07:00
|
|
|
try {
|
2016-11-12 14:08:58 +01:00
|
|
|
const moduleType = this._createCompilerAndModule();
|
2017-04-26 09:24:42 -07:00
|
|
|
this._moduleFactory =
|
|
|
|
this._compiler.compileModuleAndAllComponentsSync(moduleType).ngModuleFactory;
|
2016-07-28 04:54:49 -07:00
|
|
|
} catch (e) {
|
2017-01-27 13:19:00 -08:00
|
|
|
if (getComponentType(e)) {
|
2016-07-28 04:54:49 -07:00
|
|
|
throw new Error(
|
2017-01-27 13:19:00 -08:00
|
|
|
`This test module uses the component ${stringify(getComponentType(e))} which is using a "templateUrl" or "styleUrls", but they were never compiled. ` +
|
2016-07-28 04:54:49 -07:00
|
|
|
`Please call "TestBed.compileComponents" before your test.`);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
}
|
2016-12-09 13:04:18 -08:00
|
|
|
const ngZone = new NgZone({enableLongStackTrace: true});
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 12:33:29 -07:00
|
|
|
const ngZoneInjector =
|
|
|
|
Injector.create([{provide: NgZone, useValue: ngZone}], this.platform.injector);
|
2017-04-26 09:24:42 -07:00
|
|
|
this._moduleRef = this._moduleFactory.create(ngZoneInjector);
|
2017-05-16 15:14:55 -07:00
|
|
|
// ApplicationInitStatus.runInitializers() is marked @internal to core. So casting to any
|
|
|
|
// before accessing it.
|
|
|
|
(this._moduleRef.injector.get(ApplicationInitStatus) as any).runInitializers();
|
2016-07-28 04:54:49 -07:00
|
|
|
this._instantiated = true;
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
}
|
|
|
|
|
2016-08-10 18:21:28 -07:00
|
|
|
private _createCompilerAndModule(): Type<any> {
|
2016-07-18 03:50:31 -07:00
|
|
|
const providers = this._providers.concat([{provide: TestBed, useValue: this}]);
|
|
|
|
const declarations = this._declarations;
|
|
|
|
const imports = [this.ngModule, this._imports];
|
2016-07-25 03:02:57 -07:00
|
|
|
const schemas = this._schemas;
|
2016-07-18 03:50:31 -07:00
|
|
|
|
2017-03-14 16:26:17 -07:00
|
|
|
@NgModule({providers, declarations, imports, schemas})
|
2016-07-18 03:50:31 -07:00
|
|
|
class DynamicTestModule {
|
|
|
|
}
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
const compilerFactory: TestingCompilerFactory =
|
|
|
|
this.platform.injector.get(TestingCompilerFactory);
|
2016-07-18 03:50:31 -07:00
|
|
|
this._compiler =
|
2016-07-28 04:54:49 -07:00
|
|
|
compilerFactory.createTestingCompiler(this._compilerOptions.concat([{useDebug: true}]));
|
2017-04-26 09:24:42 -07:00
|
|
|
this._compiler.loadAotSummaries(this._aotSummaries);
|
2016-07-28 04:54:49 -07:00
|
|
|
this._moduleOverrides.forEach((entry) => this._compiler.overrideModule(entry[0], entry[1]));
|
|
|
|
this._componentOverrides.forEach(
|
|
|
|
(entry) => this._compiler.overrideComponent(entry[0], entry[1]));
|
|
|
|
this._directiveOverrides.forEach(
|
|
|
|
(entry) => this._compiler.overrideDirective(entry[0], entry[1]));
|
|
|
|
this._pipeOverrides.forEach((entry) => this._compiler.overridePipe(entry[0], entry[1]));
|
2016-07-18 03:50:31 -07:00
|
|
|
return DynamicTestModule;
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
}
|
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
private _assertNotInstantiated(methodName: string, methodDescription: string) {
|
|
|
|
if (this._instantiated) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error(
|
2016-07-28 04:54:49 -07:00
|
|
|
`Cannot ${methodDescription} when the test module has already been instantiated. ` +
|
|
|
|
`Make sure you are not using \`inject\` before \`${methodName}\`.`);
|
|
|
|
}
|
2015-12-08 19:03:21 -08:00
|
|
|
}
|
|
|
|
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) {
|
2016-07-28 04:54:49 -07:00
|
|
|
this._initIfNeeded();
|
2016-07-20 10:51:21 -07:00
|
|
|
if (token === TestBed) {
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
return this;
|
|
|
|
}
|
2016-07-18 03:50:31 -07:00
|
|
|
// Tests can inject things from the ng module and from the compiler,
|
|
|
|
// but the ng module can't inject things from the compiler and vice versa.
|
2016-09-16 10:57:57 -07:00
|
|
|
const result = this._moduleRef.injector.get(token, UNDEFINED);
|
2016-07-28 04:54:49 -07:00
|
|
|
return result === UNDEFINED ? this._compiler.injector.get(token, notFoundValue) : result;
|
2016-04-26 13:06:50 -07:00
|
|
|
}
|
|
|
|
|
2016-12-31 11:50:03 +01:00
|
|
|
execute(tokens: any[], fn: Function, context?: any): any {
|
2016-07-28 04:54:49 -07:00
|
|
|
this._initIfNeeded();
|
2016-09-16 10:57:57 -07:00
|
|
|
const params = tokens.map(t => this.get(t));
|
2016-12-31 11:50:03 +01:00
|
|
|
return fn.apply(context, params);
|
2015-12-08 19:03:21 -08:00
|
|
|
}
|
2016-07-28 04:54:49 -07:00
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void {
|
2016-07-28 04:54:49 -07:00
|
|
|
this._assertNotInstantiated('overrideModule', 'override module metadata');
|
|
|
|
this._moduleOverrides.push([ngModule, override]);
|
|
|
|
}
|
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void {
|
2016-07-28 04:54:49 -07:00
|
|
|
this._assertNotInstantiated('overrideComponent', 'override component metadata');
|
|
|
|
this._componentOverrides.push([component, override]);
|
|
|
|
}
|
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void {
|
2016-07-28 04:54:49 -07:00
|
|
|
this._assertNotInstantiated('overrideDirective', 'override directive metadata');
|
|
|
|
this._directiveOverrides.push([directive, override]);
|
|
|
|
}
|
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void {
|
2016-07-28 04:54:49 -07:00
|
|
|
this._assertNotInstantiated('overridePipe', 'override pipe metadata');
|
|
|
|
this._pipeOverrides.push([pipe, override]);
|
|
|
|
}
|
|
|
|
|
2017-05-15 13:12:10 -07:00
|
|
|
/**
|
|
|
|
* Overwrites all providers for the given token with the given provider definition.
|
|
|
|
*/
|
|
|
|
overrideProvider(token: any, provider: {
|
|
|
|
useFactory: Function,
|
|
|
|
deps: any[],
|
|
|
|
}): void;
|
|
|
|
overrideProvider(token: any, provider: {useValue: any;}): void;
|
|
|
|
overrideProvider(token: any, provider: {
|
|
|
|
useFactory?: Function,
|
|
|
|
useValue?: any,
|
|
|
|
deps?: any[],
|
|
|
|
}): void {
|
|
|
|
let flags: NodeFlags = 0;
|
|
|
|
let value: any;
|
|
|
|
if (provider.useFactory) {
|
|
|
|
flags |= NodeFlags.TypeFactoryProvider;
|
|
|
|
value = provider.useFactory;
|
|
|
|
} else {
|
|
|
|
flags |= NodeFlags.TypeValueProvider;
|
|
|
|
value = provider.useValue;
|
|
|
|
}
|
|
|
|
const deps = (provider.deps || []).map((dep) => {
|
|
|
|
let depFlags: DepFlags = DepFlags.None;
|
|
|
|
let depToken: any;
|
|
|
|
if (Array.isArray(dep)) {
|
|
|
|
dep.forEach((entry: any) => {
|
|
|
|
if (entry instanceof Optional) {
|
|
|
|
depFlags |= DepFlags.Optional;
|
|
|
|
} else if (entry instanceof SkipSelf) {
|
|
|
|
depFlags |= DepFlags.SkipSelf;
|
|
|
|
} else {
|
|
|
|
depToken = entry;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
depToken = dep;
|
|
|
|
}
|
|
|
|
return [depFlags, depToken];
|
|
|
|
});
|
|
|
|
overrideProvider({token, flags, deps, value});
|
|
|
|
}
|
|
|
|
|
2016-08-10 18:21:28 -07:00
|
|
|
createComponent<T>(component: Type<T>): ComponentFixture<T> {
|
2016-07-28 04:54:49 -07:00
|
|
|
this._initIfNeeded();
|
2017-04-26 09:24:42 -07:00
|
|
|
const componentFactory = this._compiler.getComponentFactory(component);
|
2017-03-14 16:26:17 -07:00
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
if (!componentFactory) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error(
|
2016-07-28 04:54:49 -07:00
|
|
|
`Cannot create the component ${stringify(component)} as it was not imported into the testing module!`);
|
|
|
|
}
|
2017-03-14 16:26:17 -07:00
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
const noNgZone = this.get(ComponentFixtureNoNgZone, false);
|
|
|
|
const autoDetect: boolean = this.get(ComponentFixtureAutoDetect, false);
|
|
|
|
const ngZone: NgZone = noNgZone ? null : this.get(NgZone, null);
|
|
|
|
const testComponentRenderer: TestComponentRenderer = this.get(TestComponentRenderer);
|
|
|
|
const rootElId = `root${_nextRootElementId++}`;
|
|
|
|
testComponentRenderer.insertRootElement(rootElId);
|
|
|
|
|
|
|
|
const initComponent = () => {
|
2017-03-14 16:26:17 -07:00
|
|
|
const componentRef =
|
|
|
|
componentFactory.create(Injector.NULL, [], `#${rootElId}`, this._moduleRef);
|
2016-07-28 04:54:49 -07:00
|
|
|
return new ComponentFixture<T>(componentRef, ngZone, autoDetect);
|
|
|
|
};
|
|
|
|
|
2016-09-16 10:57:57 -07:00
|
|
|
const fixture = !ngZone ? initComponent() : ngZone.run(initComponent);
|
2016-08-25 23:37:46 +02:00
|
|
|
this._activeFixtures.push(fixture);
|
|
|
|
return fixture;
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
2015-12-08 19:03:21 -08:00
|
|
|
}
|
|
|
|
|
2017-03-29 09:34:45 -07:00
|
|
|
let _testBed: TestBed = null !;
|
2015-12-08 19:03:21 -08:00
|
|
|
|
2016-06-27 12:27:23 -07:00
|
|
|
/**
|
|
|
|
* @experimental
|
|
|
|
*/
|
2016-07-20 10:51:21 -07:00
|
|
|
export function getTestBed() {
|
2016-09-16 10:57:57 -07:00
|
|
|
return _testBed = _testBed || new TestBed();
|
2016-07-20 10:51:21 -07:00
|
|
|
}
|
|
|
|
|
2015-03-13 20:52:59 +00:00
|
|
|
/**
|
2015-12-08 16:44:04 -08:00
|
|
|
* Allows injecting dependencies in `beforeEach()` and `it()`.
|
2015-03-13 11:10:11 +01:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
2015-03-13 18:48:29 +01:00
|
|
|
* ```
|
|
|
|
* beforeEach(inject([Dependency, AClass], (dep, object) => {
|
|
|
|
* // some code that uses `dep` and `object`
|
|
|
|
* // ...
|
|
|
|
* }));
|
2015-03-13 11:10:11 +01:00
|
|
|
*
|
2015-11-18 18:46:24 -08:00
|
|
|
* it('...', inject([AClass], (object) => {
|
2015-12-08 16:44:04 -08:00
|
|
|
* object.doSomething();
|
|
|
|
* expect(...);
|
2015-03-13 18:48:29 +01:00
|
|
|
* })
|
|
|
|
* ```
|
2015-03-13 11:10:11 +01:00
|
|
|
*
|
|
|
|
* Notes:
|
2015-12-08 19:03:21 -08:00
|
|
|
* - inject is currently a function because of some Traceur limitation the syntax should
|
|
|
|
* eventually
|
2015-03-13 11:10:11 +01:00
|
|
|
* becomes `it('...', @Inject (object: AClass, async: AsyncTestCompleter) => { ... });`
|
|
|
|
*
|
2016-06-27 12:27:23 -07:00
|
|
|
* @stable
|
2015-03-13 11:10:11 +01:00
|
|
|
*/
|
refactor(testing): remove wrapping of Jasmine functions (#9564)
Instead, the async function now determines whether it should return a promise
or instead call a done function parameter. Importing Jasmine functions
from `@angular/core/testing` is no longer necessary and is now deprecated.
Additionally, beforeEachProviders is also deprecated, as it is specific
to the testing framework. Instead, use the new addProviders method directly.
Before:
```js
import {beforeEachProviders, it, describe, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEachProviders(() => [MyService]);
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
After:
```js
import {addProviders, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEach(() => {
addProviders([MyService]);
});
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
2016-06-24 17:48:35 -07:00
|
|
|
export function inject(tokens: any[], fn: Function): () => any {
|
2016-09-16 10:57:57 -07:00
|
|
|
const testBed = getTestBed();
|
2016-04-26 13:06:50 -07:00
|
|
|
if (tokens.indexOf(AsyncTestCompleter) >= 0) {
|
2016-12-31 11:50:03 +01:00
|
|
|
// Not using an arrow function to preserve context passed from call site
|
|
|
|
return function() {
|
|
|
|
// Return an async test method that returns a Promise if AsyncTestCompleter is one of
|
|
|
|
// the injected tokens.
|
|
|
|
return testBed.compileComponents().then(() => {
|
|
|
|
const completer: AsyncTestCompleter = testBed.get(AsyncTestCompleter);
|
|
|
|
testBed.execute(tokens, fn, this);
|
|
|
|
return completer.promise;
|
|
|
|
});
|
|
|
|
};
|
2016-04-26 13:06:50 -07:00
|
|
|
} else {
|
2016-12-31 11:50:03 +01:00
|
|
|
// Not using an arrow function to preserve context passed from call site
|
|
|
|
return function() { return testBed.execute(tokens, fn, this); };
|
2016-04-26 13:06:50 -07:00
|
|
|
}
|
2015-10-08 15:33:17 -07:00
|
|
|
}
|
|
|
|
|
2016-06-27 12:27:23 -07:00
|
|
|
/**
|
|
|
|
* @experimental
|
|
|
|
*/
|
2015-12-10 12:00:48 -08:00
|
|
|
export class InjectSetupWrapper {
|
2016-07-28 04:54:49 -07:00
|
|
|
constructor(private _moduleDef: () => TestModuleMetadata) {}
|
2015-12-10 12:00:48 -08:00
|
|
|
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
private _addModule() {
|
2016-07-28 04:54:49 -07:00
|
|
|
const moduleDef = this._moduleDef();
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
if (moduleDef) {
|
2016-07-28 04:54:49 -07:00
|
|
|
getTestBed().configureTestingModule(moduleDef);
|
2016-04-26 13:06:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
refactor(testing): remove wrapping of Jasmine functions (#9564)
Instead, the async function now determines whether it should return a promise
or instead call a done function parameter. Importing Jasmine functions
from `@angular/core/testing` is no longer necessary and is now deprecated.
Additionally, beforeEachProviders is also deprecated, as it is specific
to the testing framework. Instead, use the new addProviders method directly.
Before:
```js
import {beforeEachProviders, it, describe, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEachProviders(() => [MyService]);
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
After:
```js
import {addProviders, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEach(() => {
addProviders([MyService]);
});
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
2016-06-24 17:48:35 -07:00
|
|
|
inject(tokens: any[], fn: Function): () => any {
|
2016-12-31 11:50:03 +01:00
|
|
|
const self = this;
|
|
|
|
// Not using an arrow function to preserve context passed from call site
|
|
|
|
return function() {
|
|
|
|
self._addModule();
|
|
|
|
return inject(tokens, fn).call(this);
|
2016-05-26 13:33:53 -07:00
|
|
|
};
|
2015-12-10 12:00:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 (3f55aa609f60f130f1d69188ed057214b1267cb3).
- 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
2016-07-04 09:37:30 -07:00
|
|
|
/**
|
|
|
|
* @experimental
|
|
|
|
*/
|
2016-07-28 04:54:49 -07:00
|
|
|
export function withModule(moduleDef: TestModuleMetadata): InjectSetupWrapper;
|
|
|
|
export function withModule(moduleDef: TestModuleMetadata, fn: Function): () => any;
|
2017-03-29 09:34:45 -07:00
|
|
|
export function withModule(moduleDef: TestModuleMetadata, fn?: Function | null): (() => any)|
|
2016-07-28 04:54:49 -07:00
|
|
|
InjectSetupWrapper {
|
|
|
|
if (fn) {
|
2016-12-31 11:50:03 +01:00
|
|
|
// Not using an arrow function to preserve context passed from call site
|
|
|
|
return function() {
|
2016-07-28 04:54:49 -07:00
|
|
|
const testBed = getTestBed();
|
|
|
|
if (moduleDef) {
|
|
|
|
testBed.configureTestingModule(moduleDef);
|
|
|
|
}
|
2016-12-31 11:50:03 +01:00
|
|
|
return fn.apply(this);
|
2016-07-28 04:54:49 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return new InjectSetupWrapper(() => moduleDef);
|
2015-12-10 12:00:48 -08:00
|
|
|
}
|
2017-01-27 13:19:00 -08:00
|
|
|
|
|
|
|
function getComponentType(error: Error): Function {
|
2017-02-17 12:55:55 -08:00
|
|
|
return (error as any)[ɵERROR_COMPONENT_TYPE];
|
2017-01-27 13:19:00 -08:00
|
|
|
}
|