refactor(testing): introduce new testing api to support ng modules

BREAKING CHANGE:
- deprecations:
  * `withProviders`, use `TestBed.withModule` instead
  * `addProviders`, use `TestBed.configureTestingModule` instead
  * `TestComponentBuilder`, use `TestBed.configureTestModule` / `TestBed.override...` / `TestBed.createComponent` instead.

Closes #10354
This commit is contained in:
Tobias Bosch
2016-07-28 04:54:49 -07:00
parent acc6c8d0b7
commit d0a95e35af
45 changed files with 1090 additions and 501 deletions

View File

@ -13,75 +13,25 @@
*/
import {SchemaMetadata} from '../index';
import {TestBed, getTestBed} from './test_bed';
import {TestBed, TestModuleMetadata, getTestBed} from './test_bed';
declare var global: any;
var _global = <any>(typeof window === 'undefined' ? global : window);
var testBed: TestBed = getTestBed();
// Reset the test providers before each test.
if (_global.beforeEach) {
_global.beforeEach(() => { testBed.reset(); });
_global.beforeEach(() => { TestBed.resetTestingModule(); });
}
/**
* Allows overriding default providers of the test injector,
* which are defined in test_injector.js
*
* @stable
* @deprecated Use `TestBed.configureTestingModule instead.
*/
export function addProviders(providers: Array<any>): void {
if (!providers) return;
try {
testBed.configureModule({providers: providers});
} catch (e) {
throw new Error(
'addProviders can\'t be called after the injector has been already created for this test. ' +
'This is most likely because you\'ve already used the injector to inject a beforeEach or the ' +
'current `it` function.');
}
}
/**
* Allows overriding default providers, directives, pipes, modules of the test injector,
* which are defined in test_injector.js
*
* @stable
*/
export function configureModule(moduleDef: {
providers?: any[],
declarations?: any[],
imports?: any[],
entryComponents?: any[],
schemas?: Array<SchemaMetadata|any[]>
}): void {
if (!moduleDef) return;
try {
testBed.configureModule(moduleDef);
} catch (e) {
throw new Error(
'configureModule can\'t be called after the injector has been already created for this test. ' +
'This is most likely because you\'ve already used the injector to inject a beforeEach or the ' +
'current `it` function.');
}
}
/**
* Allows overriding default compiler providers and settings
* which are defined in test_injector.js
*
* @stable
*/
export function configureCompiler(config: {providers?: any[], useJit?: boolean}): void {
if (!config) return;
try {
testBed.configureCompiler(config);
} catch (e) {
throw new Error(
'configureCompiler can\'t be called after the injector has been already created for this test. ' +
'This is most likely because you\'ve already used the injector to inject a beforeEach or the ' +
'current `it` function.');
}
TestBed.configureTestingModule({providers: providers});
}