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
|
|
|
|
|
*/
|
|
|
|
|
|
2015-10-08 15:33:17 -07:00
|
|
|
/**
|
2016-06-24 17:48:35 -07:00
|
|
|
* Public Test Library for unit testing Angular2 Applications. Assumes that you are running
|
|
|
|
|
* with Jasmine, Mocha, or a similar framework which exports a beforeEach function and
|
|
|
|
|
* allows tests to be asynchronous by either returning a promise or using a 'done' parameter.
|
2015-10-08 15:33:17 -07:00
|
|
|
*/
|
|
|
|
|
|
2016-07-20 10:51:21 -07:00
|
|
|
import {TestBed, getTestBed} from './test_bed';
|
2015-10-08 15:33:17 -07:00
|
|
|
|
2016-06-15 09:37:33 -07:00
|
|
|
declare var global: any;
|
2015-10-08 15:33:17 -07:00
|
|
|
|
2016-02-01 10:28:57 -08:00
|
|
|
var _global = <any>(typeof window === 'undefined' ? global : window);
|
2015-10-08 15:33:17 -07:00
|
|
|
|
2016-07-20 10:51:21 -07:00
|
|
|
var testBed: TestBed = getTestBed();
|
2016-06-24 17:48:35 -07:00
|
|
|
|
|
|
|
|
// Reset the test providers before each test.
|
|
|
|
|
if (_global.beforeEach) {
|
2016-07-20 10:51:21 -07:00
|
|
|
_global.beforeEach(() => { testBed.reset(); });
|
2015-10-08 15:33:17 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-18 18:46:24 -08:00
|
|
|
/**
|
2016-06-24 17:48:35 -07:00
|
|
|
* Allows overriding default providers of the test injector,
|
|
|
|
|
* which are defined in test_injector.js
|
2016-06-27 12:27:23 -07:00
|
|
|
*
|
|
|
|
|
* @stable
|
2015-11-18 18:46:24 -08:00
|
|
|
*/
|
2016-06-24 17:48:35 -07:00
|
|
|
export function addProviders(providers: Array<any>): void {
|
|
|
|
|
if (!providers) return;
|
|
|
|
|
try {
|
2016-07-20 10:51:21 -07:00
|
|
|
testBed.configureModule({providers: providers});
|
2016-06-24 17:48:35 -07:00
|
|
|
} 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.');
|
|
|
|
|
}
|
2015-10-08 15:33:17 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 09:37:30 -07:00
|
|
|
/**
|
|
|
|
|
* 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[],
|
|
|
|
|
directives?: any[],
|
|
|
|
|
pipes?: any[],
|
|
|
|
|
precompile?: any[],
|
|
|
|
|
modules?: any[]
|
|
|
|
|
}): void {
|
|
|
|
|
if (!moduleDef) return;
|
|
|
|
|
try {
|
2016-07-20 10:51:21 -07:00
|
|
|
testBed.configureModule(moduleDef);
|
2016-07-04 09:37:30 -07:00
|
|
|
} 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 {
|
2016-07-20 10:51:21 -07:00
|
|
|
testBed.configureCompiler(config);
|
2016-07-04 09:37:30 -07:00
|
|
|
} 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.');
|
|
|
|
|
}
|
|
|
|
|
}
|