refactor: move angular source to /packages rather than modules/@angular
This commit is contained in:
112
packages/core/testing/src/async.ts
Normal file
112
packages/core/testing/src/async.ts
Normal file
@ -0,0 +1,112 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
declare var global: any;
|
||||
|
||||
const _global = <any>(typeof window === 'undefined' ? global : window);
|
||||
|
||||
/**
|
||||
* Wraps a test function in an asynchronous test zone. The test will automatically
|
||||
* complete when all asynchronous calls within this zone are done. Can be used
|
||||
* to wrap an {@link inject} call.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```
|
||||
* it('...', async(inject([AClass], (object) => {
|
||||
* object.doSomething.then(() => {
|
||||
* expect(...);
|
||||
* })
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @stable
|
||||
*/
|
||||
export function async(fn: Function): (done: any) => any {
|
||||
// If we're running using the Jasmine test framework, adapt to call the 'done'
|
||||
// function when asynchronous activity is finished.
|
||||
if (_global.jasmine) {
|
||||
// Not using an arrow function to preserve context passed from call site
|
||||
return function(done: any) {
|
||||
if (!done) {
|
||||
// if we run beforeEach in @angular/core/testing/testing_internal then we get no done
|
||||
// fake it here and assume sync.
|
||||
done = function() {};
|
||||
done.fail = function(e: any) { throw e; };
|
||||
}
|
||||
runInTestZone(fn, this, done, (err: any) => {
|
||||
if (typeof err === 'string') {
|
||||
return done.fail(new Error(<string>err));
|
||||
} else {
|
||||
done.fail(err);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
// Otherwise, return a promise which will resolve when asynchronous activity
|
||||
// is finished. This will be correctly consumed by the Mocha framework with
|
||||
// it('...', async(myFn)); or can be used in a custom framework.
|
||||
// Not using an arrow function to preserve context passed from call site
|
||||
return function() {
|
||||
return new Promise<void>((finishCallback, failCallback) => {
|
||||
runInTestZone(fn, this, finishCallback, failCallback);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function runInTestZone(
|
||||
fn: Function, context: any, finishCallback: Function, failCallback: Function) {
|
||||
const currentZone = Zone.current;
|
||||
const AsyncTestZoneSpec = (Zone as any)['AsyncTestZoneSpec'];
|
||||
if (AsyncTestZoneSpec === undefined) {
|
||||
throw new Error(
|
||||
'AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
|
||||
'Please make sure that your environment includes zone.js/dist/async-test.js');
|
||||
}
|
||||
const ProxyZoneSpec = (Zone as any)['ProxyZoneSpec'] as {
|
||||
get(): {setDelegate(spec: ZoneSpec): void; getDelegate(): ZoneSpec;};
|
||||
assertPresent: () => void;
|
||||
};
|
||||
if (ProxyZoneSpec === undefined) {
|
||||
throw new Error(
|
||||
'ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
|
||||
'Please make sure that your environment includes zone.js/dist/proxy.js');
|
||||
}
|
||||
const proxyZoneSpec = ProxyZoneSpec.get();
|
||||
ProxyZoneSpec.assertPresent();
|
||||
// We need to create the AsyncTestZoneSpec outside the ProxyZone.
|
||||
// If we do it in ProxyZone then we will get to infinite recursion.
|
||||
const proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
|
||||
const previousDelegate = proxyZoneSpec.getDelegate();
|
||||
proxyZone.parent.run(() => {
|
||||
const testZoneSpec: ZoneSpec = new AsyncTestZoneSpec(
|
||||
() => {
|
||||
// Need to restore the original zone.
|
||||
currentZone.run(() => {
|
||||
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
||||
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
|
||||
proxyZoneSpec.setDelegate(previousDelegate);
|
||||
}
|
||||
finishCallback();
|
||||
});
|
||||
},
|
||||
(error: any) => {
|
||||
// Need to restore the original zone.
|
||||
currentZone.run(() => {
|
||||
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
||||
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
|
||||
proxyZoneSpec.setDelegate(previousDelegate);
|
||||
}
|
||||
failCallback(error);
|
||||
});
|
||||
},
|
||||
'test');
|
||||
proxyZoneSpec.setDelegate(testZoneSpec);
|
||||
});
|
||||
return Zone.current.runGuarded(fn, context);
|
||||
}
|
24
packages/core/testing/src/async_test_completer.ts
Normal file
24
packages/core/testing/src/async_test_completer.ts
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Injectable completer that allows signaling completion of an asynchronous test. Used internally.
|
||||
*/
|
||||
export class AsyncTestCompleter {
|
||||
private _resolve: (result: any) => void;
|
||||
private _reject: (err: any) => void;
|
||||
private _promise: Promise<any> = new Promise((res, rej) => {
|
||||
this._resolve = res;
|
||||
this._reject = rej;
|
||||
});
|
||||
done(value?: any) { this._resolve(value); }
|
||||
|
||||
fail(error?: any, stackTrace?: string) { this._reject(error); }
|
||||
|
||||
get promise(): Promise<any> { return this._promise; }
|
||||
}
|
32
packages/core/testing/src/before_each.ts
Normal file
32
packages/core/testing/src/before_each.ts
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import {resetFakeAsyncZone} from './fake_async';
|
||||
import {TestBed} from './test_bed';
|
||||
|
||||
declare var global: any;
|
||||
|
||||
const _global = <any>(typeof window === 'undefined' ? global : window);
|
||||
|
||||
// Reset the test providers and the fake async zone before each test.
|
||||
if (_global.beforeEach) {
|
||||
_global.beforeEach(() => {
|
||||
TestBed.resetTestingModule();
|
||||
resetFakeAsyncZone();
|
||||
});
|
||||
}
|
||||
|
||||
// TODO(juliemr): remove this, only used because we need to export something to have compilation
|
||||
// work.
|
||||
export const __core_private_testing_placeholder__ = '';
|
191
packages/core/testing/src/component_fixture.ts
Normal file
191
packages/core/testing/src/component_fixture.ts
Normal file
@ -0,0 +1,191 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {ChangeDetectorRef, ComponentRef, DebugElement, ElementRef, NgZone, getDebugNode} from '@angular/core';
|
||||
|
||||
|
||||
/**
|
||||
* Fixture for debugging and testing a component.
|
||||
*
|
||||
* @stable
|
||||
*/
|
||||
export class ComponentFixture<T> {
|
||||
/**
|
||||
* The DebugElement associated with the root element of this component.
|
||||
*/
|
||||
debugElement: DebugElement;
|
||||
|
||||
/**
|
||||
* The instance of the root component class.
|
||||
*/
|
||||
componentInstance: T;
|
||||
|
||||
/**
|
||||
* The native element at the root of the component.
|
||||
*/
|
||||
nativeElement: any;
|
||||
|
||||
/**
|
||||
* The ElementRef for the element at the root of the component.
|
||||
*/
|
||||
elementRef: ElementRef;
|
||||
|
||||
/**
|
||||
* The ChangeDetectorRef for the component
|
||||
*/
|
||||
changeDetectorRef: ChangeDetectorRef;
|
||||
|
||||
private _isStable: boolean = true;
|
||||
private _isDestroyed: boolean = false;
|
||||
private _resolve: (result: any) => void;
|
||||
private _promise: Promise<any> = null;
|
||||
private _onUnstableSubscription: any /** TODO #9100 */ = null;
|
||||
private _onStableSubscription: any /** TODO #9100 */ = null;
|
||||
private _onMicrotaskEmptySubscription: any /** TODO #9100 */ = null;
|
||||
private _onErrorSubscription: any /** TODO #9100 */ = null;
|
||||
|
||||
constructor(
|
||||
public componentRef: ComponentRef<T>, public ngZone: NgZone, private _autoDetect: boolean) {
|
||||
this.changeDetectorRef = componentRef.changeDetectorRef;
|
||||
this.elementRef = componentRef.location;
|
||||
this.debugElement = <DebugElement>getDebugNode(this.elementRef.nativeElement);
|
||||
this.componentInstance = componentRef.instance;
|
||||
this.nativeElement = this.elementRef.nativeElement;
|
||||
this.componentRef = componentRef;
|
||||
this.ngZone = ngZone;
|
||||
|
||||
if (ngZone != null) {
|
||||
this._onUnstableSubscription =
|
||||
ngZone.onUnstable.subscribe({next: () => { this._isStable = false; }});
|
||||
this._onMicrotaskEmptySubscription = ngZone.onMicrotaskEmpty.subscribe({
|
||||
next: () => {
|
||||
if (this._autoDetect) {
|
||||
// Do a change detection run with checkNoChanges set to true to check
|
||||
// there are no changes on the second run.
|
||||
this.detectChanges(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
this._onStableSubscription = ngZone.onStable.subscribe({
|
||||
next: () => {
|
||||
this._isStable = true;
|
||||
// Check whether there is a pending whenStable() completer to resolve.
|
||||
if (this._promise !== null) {
|
||||
// If so check whether there are no pending macrotasks before resolving.
|
||||
// Do this check in the next tick so that ngZone gets a chance to update the state of
|
||||
// pending macrotasks.
|
||||
scheduleMicroTask(() => {
|
||||
if (!this.ngZone.hasPendingMacrotasks) {
|
||||
if (this._promise !== null) {
|
||||
this._resolve(true);
|
||||
this._resolve = null;
|
||||
this._promise = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this._onErrorSubscription =
|
||||
ngZone.onError.subscribe({next: (error: any) => { throw error; }});
|
||||
}
|
||||
}
|
||||
|
||||
private _tick(checkNoChanges: boolean) {
|
||||
this.changeDetectorRef.detectChanges();
|
||||
if (checkNoChanges) {
|
||||
this.checkNoChanges();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger a change detection cycle for the component.
|
||||
*/
|
||||
detectChanges(checkNoChanges: boolean = true): void {
|
||||
if (this.ngZone != null) {
|
||||
// Run the change detection inside the NgZone so that any async tasks as part of the change
|
||||
// detection are captured by the zone and can be waited for in isStable.
|
||||
this.ngZone.run(() => { this._tick(checkNoChanges); });
|
||||
} else {
|
||||
// Running without zone. Just do the change detection.
|
||||
this._tick(checkNoChanges);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do a change detection run to make sure there were no changes.
|
||||
*/
|
||||
checkNoChanges(): void { this.changeDetectorRef.checkNoChanges(); }
|
||||
|
||||
/**
|
||||
* Set whether the fixture should autodetect changes.
|
||||
*
|
||||
* Also runs detectChanges once so that any existing change is detected.
|
||||
*/
|
||||
autoDetectChanges(autoDetect: boolean = true) {
|
||||
if (this.ngZone == null) {
|
||||
throw new Error('Cannot call autoDetectChanges when ComponentFixtureNoNgZone is set');
|
||||
}
|
||||
this._autoDetect = autoDetect;
|
||||
this.detectChanges();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether the fixture is currently stable or has async tasks that have not been completed
|
||||
* yet.
|
||||
*/
|
||||
isStable(): boolean { return this._isStable && !this.ngZone.hasPendingMacrotasks; }
|
||||
|
||||
/**
|
||||
* Get a promise that resolves when the fixture is stable.
|
||||
*
|
||||
* This can be used to resume testing after events have triggered asynchronous activity or
|
||||
* asynchronous change detection.
|
||||
*/
|
||||
whenStable(): Promise<any> {
|
||||
if (this.isStable()) {
|
||||
return Promise.resolve(false);
|
||||
} else if (this._promise !== null) {
|
||||
return this._promise;
|
||||
} else {
|
||||
this._promise = new Promise(res => { this._resolve = res; });
|
||||
return this._promise;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger component destruction.
|
||||
*/
|
||||
destroy(): void {
|
||||
if (!this._isDestroyed) {
|
||||
this.componentRef.destroy();
|
||||
if (this._onUnstableSubscription != null) {
|
||||
this._onUnstableSubscription.unsubscribe();
|
||||
this._onUnstableSubscription = null;
|
||||
}
|
||||
if (this._onStableSubscription != null) {
|
||||
this._onStableSubscription.unsubscribe();
|
||||
this._onStableSubscription = null;
|
||||
}
|
||||
if (this._onMicrotaskEmptySubscription != null) {
|
||||
this._onMicrotaskEmptySubscription.unsubscribe();
|
||||
this._onMicrotaskEmptySubscription = null;
|
||||
}
|
||||
if (this._onErrorSubscription != null) {
|
||||
this._onErrorSubscription.unsubscribe();
|
||||
this._onErrorSubscription = null;
|
||||
}
|
||||
this._isDestroyed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleMicroTask(fn: Function) {
|
||||
Zone.current.scheduleMicroTask('scheduleMicrotask', fn);
|
||||
}
|
136
packages/core/testing/src/fake_async.ts
Normal file
136
packages/core/testing/src/fake_async.ts
Normal file
@ -0,0 +1,136 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
|
||||
const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
|
||||
type ProxyZoneSpec = {
|
||||
setDelegate(delegateSpec: ZoneSpec): void; getDelegate(): ZoneSpec; resetDelegate(): void;
|
||||
};
|
||||
const ProxyZoneSpec: {get(): ProxyZoneSpec; assertPresent: () => ProxyZoneSpec} =
|
||||
(Zone as any)['ProxyZoneSpec'];
|
||||
|
||||
let _fakeAsyncTestZoneSpec: any = null;
|
||||
|
||||
/**
|
||||
* Clears out the shared fake async zone for a test.
|
||||
* To be called in a global `beforeEach`.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export function resetFakeAsyncZone() {
|
||||
_fakeAsyncTestZoneSpec = null;
|
||||
ProxyZoneSpec.assertPresent().resetDelegate();
|
||||
}
|
||||
|
||||
let _inFakeAsyncCall = false;
|
||||
|
||||
/**
|
||||
* Wraps a function to be executed in the fakeAsync zone:
|
||||
* - microtasks are manually executed by calling `flushMicrotasks()`,
|
||||
* - timers are synchronous, `tick()` simulates the asynchronous passage of time.
|
||||
*
|
||||
* If there are any pending timers at the end of the function, an exception will be thrown.
|
||||
*
|
||||
* Can be used to wrap inject() calls.
|
||||
*
|
||||
* ## Example
|
||||
*
|
||||
* {@example testing/ts/fake_async.ts region='basic'}
|
||||
*
|
||||
* @param fn
|
||||
* @returns {Function} The function wrapped to be executed in the fakeAsync zone
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export function fakeAsync(fn: Function): (...args: any[]) => any {
|
||||
// Not using an arrow function to preserve context passed from call site
|
||||
return function(...args: any[]) {
|
||||
const proxyZoneSpec = ProxyZoneSpec.assertPresent();
|
||||
if (_inFakeAsyncCall) {
|
||||
throw new Error('fakeAsync() calls can not be nested');
|
||||
}
|
||||
_inFakeAsyncCall = true;
|
||||
try {
|
||||
if (!_fakeAsyncTestZoneSpec) {
|
||||
if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) {
|
||||
throw new Error('fakeAsync() calls can not be nested');
|
||||
}
|
||||
|
||||
_fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
|
||||
}
|
||||
|
||||
let res: any;
|
||||
const lastProxyZoneSpec = proxyZoneSpec.getDelegate();
|
||||
proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
|
||||
try {
|
||||
res = fn.apply(this, args);
|
||||
flushMicrotasks();
|
||||
} finally {
|
||||
proxyZoneSpec.setDelegate(lastProxyZoneSpec);
|
||||
}
|
||||
|
||||
if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
|
||||
throw new Error(
|
||||
`${_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length} ` +
|
||||
`periodic timer(s) still in the queue.`);
|
||||
}
|
||||
|
||||
if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
|
||||
throw new Error(
|
||||
`${_fakeAsyncTestZoneSpec.pendingTimers.length} timer(s) still in the queue.`);
|
||||
}
|
||||
return res;
|
||||
} finally {
|
||||
_inFakeAsyncCall = false;
|
||||
resetFakeAsyncZone();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function _getFakeAsyncZoneSpec(): any {
|
||||
if (_fakeAsyncTestZoneSpec == null) {
|
||||
throw new Error('The code should be running in the fakeAsync zone to call this function');
|
||||
}
|
||||
return _fakeAsyncTestZoneSpec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulates the asynchronous passage of time for the timers in the fakeAsync zone.
|
||||
*
|
||||
* The microtasks queue is drained at the very start of this function and after any timer callback
|
||||
* has been executed.
|
||||
*
|
||||
* ## Example
|
||||
*
|
||||
* {@example testing/ts/fake_async.ts region='basic'}
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export function tick(millis: number = 0): void {
|
||||
_getFakeAsyncZoneSpec().tick(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Discard all remaining periodic tasks.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export function discardPeriodicTasks(): void {
|
||||
const zoneSpec = _getFakeAsyncZoneSpec();
|
||||
const pendingTimers = zoneSpec.pendingPeriodicTimers;
|
||||
zoneSpec.pendingPeriodicTimers.length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush any pending microtasks.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export function flushMicrotasks(): void {
|
||||
_getFakeAsyncZoneSpec().flushMicrotasks();
|
||||
}
|
21
packages/core/testing/src/index.ts
Normal file
21
packages/core/testing/src/index.ts
Normal file
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
/**
|
||||
* @module
|
||||
* @description
|
||||
* Entry point for all public APIs of the core/testing package.
|
||||
*/
|
||||
|
||||
export * from './async';
|
||||
export * from './component_fixture';
|
||||
export * from './fake_async';
|
||||
export * from './test_bed';
|
||||
export * from './before_each';
|
||||
export * from './metadata_override';
|
||||
export * from './private_export_testing';
|
15
packages/core/testing/src/lang_utils.ts
Normal file
15
packages/core/testing/src/lang_utils.ts
Normal file
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
export function getTypeOf(instance: any /** TODO #9100 */) {
|
||||
return instance.constructor;
|
||||
}
|
||||
|
||||
export function instantiateType(type: Function, params: any[] = []) {
|
||||
return new (<any>type)(...params);
|
||||
}
|
28
packages/core/testing/src/logger.ts
Normal file
28
packages/core/testing/src/logger.ts
Normal file
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
|
||||
@Injectable()
|
||||
export class Log {
|
||||
logItems: any[];
|
||||
|
||||
constructor() { this.logItems = []; }
|
||||
|
||||
add(value: any /** TODO #9100 */): void { this.logItems.push(value); }
|
||||
|
||||
fn(value: any /** TODO #9100 */) {
|
||||
return (a1: any = null, a2: any = null, a3: any = null, a4: any = null, a5: any = null) => {
|
||||
this.logItems.push(value);
|
||||
};
|
||||
}
|
||||
|
||||
clear(): void { this.logItems = []; }
|
||||
|
||||
result(): string { return this.logItems.join('; '); }
|
||||
}
|
18
packages/core/testing/src/metadata_override.ts
Normal file
18
packages/core/testing/src/metadata_override.ts
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Type used for modifications to metadata
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export type MetadataOverride<T> = {
|
||||
add?: T,
|
||||
remove?: T,
|
||||
set?: T
|
||||
};
|
28
packages/core/testing/src/ng_zone_mock.ts
Normal file
28
packages/core/testing/src/ng_zone_mock.ts
Normal file
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {EventEmitter, Injectable, NgZone} from '@angular/core';
|
||||
|
||||
|
||||
/**
|
||||
* A mock implementation of {@link NgZone}.
|
||||
*/
|
||||
@Injectable()
|
||||
export class MockNgZone extends NgZone {
|
||||
private _mockOnStable: EventEmitter<any> = new EventEmitter(false);
|
||||
|
||||
constructor() { super({enableLongStackTrace: false}); }
|
||||
|
||||
get onStable() { return this._mockOnStable; }
|
||||
|
||||
run(fn: Function): any { return fn(); }
|
||||
|
||||
runOutsideAngular(fn: Function): any { return fn(); }
|
||||
|
||||
simulateZoneExit(): void { this.onStable.emit(null); }
|
||||
}
|
9
packages/core/testing/src/private_export_testing.ts
Normal file
9
packages/core/testing/src/private_export_testing.ts
Normal file
@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
export {TestingCompiler as ɵTestingCompiler, TestingCompilerFactory as ɵTestingCompilerFactory} from './test_compiler';
|
475
packages/core/testing/src/test_bed.ts
Normal file
475
packages/core/testing/src/test_bed.ts
Normal file
@ -0,0 +1,475 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {CompilerOptions, Component, Directive, InjectionToken, Injector, ModuleWithComponentFactories, NgModule, NgModuleRef, NgZone, Pipe, PlatformRef, Provider, ReflectiveInjector, SchemaMetadata, Type, ɵERROR_COMPONENT_TYPE, ɵstringify as stringify} from '@angular/core';
|
||||
import {AsyncTestCompleter} from './async_test_completer';
|
||||
import {ComponentFixture} from './component_fixture';
|
||||
import {MetadataOverride} from './metadata_override';
|
||||
import {TestingCompiler, TestingCompilerFactory} from './test_compiler';
|
||||
|
||||
const UNDEFINED = new Object();
|
||||
|
||||
/**
|
||||
* An abstract class for inserting the root test component element in a platform independent way.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class TestComponentRenderer {
|
||||
insertRootElement(rootElementId: string) {}
|
||||
}
|
||||
|
||||
let _nextRootElementId = 0;
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export const ComponentFixtureAutoDetect =
|
||||
new InjectionToken<boolean[]>('ComponentFixtureAutoDetect');
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export const ComponentFixtureNoNgZone = new InjectionToken<boolean[]>('ComponentFixtureNoNgZone');
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export type TestModuleMetadata = {
|
||||
providers?: any[],
|
||||
declarations?: any[],
|
||||
imports?: any[],
|
||||
schemas?: Array<SchemaMetadata|any[]>,
|
||||
};
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
export class TestBed implements Injector {
|
||||
/**
|
||||
* 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
|
||||
* '@angular/<platform_name>/testing'.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
static initTestEnvironment(ngModule: Type<any>, platform: PlatformRef): TestBed {
|
||||
const testBed = getTestBed();
|
||||
testBed.initTestEnvironment(ngModule, platform);
|
||||
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(); }
|
||||
|
||||
static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): typeof TestBed {
|
||||
getTestBed().overrideModule(ngModule, override);
|
||||
return TestBed;
|
||||
}
|
||||
|
||||
static overrideComponent(component: Type<any>, override: MetadataOverride<Component>):
|
||||
typeof TestBed {
|
||||
getTestBed().overrideComponent(component, override);
|
||||
return TestBed;
|
||||
}
|
||||
|
||||
static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>):
|
||||
typeof TestBed {
|
||||
getTestBed().overrideDirective(directive, override);
|
||||
return TestBed;
|
||||
}
|
||||
|
||||
static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): typeof TestBed {
|
||||
getTestBed().overridePipe(pipe, override);
|
||||
return TestBed;
|
||||
}
|
||||
|
||||
static overrideTemplate(component: Type<any>, template: string): typeof TestBed {
|
||||
getTestBed().overrideComponent(component, {set: {template, templateUrl: null}});
|
||||
return TestBed;
|
||||
}
|
||||
|
||||
static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) {
|
||||
return getTestBed().get(token, notFoundValue);
|
||||
}
|
||||
|
||||
static createComponent<T>(component: Type<T>): ComponentFixture<T> {
|
||||
return getTestBed().createComponent(component);
|
||||
}
|
||||
|
||||
private _instantiated: boolean = false;
|
||||
|
||||
private _compiler: TestingCompiler = null;
|
||||
private _moduleRef: NgModuleRef<any> = null;
|
||||
private _moduleWithComponentFactories: ModuleWithComponentFactories<any> = null;
|
||||
|
||||
private _compilerOptions: CompilerOptions[] = [];
|
||||
|
||||
private _moduleOverrides: [Type<any>, MetadataOverride<NgModule>][] = [];
|
||||
private _componentOverrides: [Type<any>, MetadataOverride<Component>][] = [];
|
||||
private _directiveOverrides: [Type<any>, MetadataOverride<Directive>][] = [];
|
||||
private _pipeOverrides: [Type<any>, MetadataOverride<Pipe>][] = [];
|
||||
|
||||
private _providers: Provider[] = [];
|
||||
private _declarations: Array<Type<any>|any[]|any> = [];
|
||||
private _imports: Array<Type<any>|any[]|any> = [];
|
||||
private _schemas: Array<SchemaMetadata|any[]> = [];
|
||||
private _activeFixtures: ComponentFixture<any>[] = [];
|
||||
|
||||
/**
|
||||
* 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
|
||||
* '@angular/<platform_name>/testing'.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
initTestEnvironment(ngModule: Type<any>, platform: PlatformRef) {
|
||||
if (this.platform || this.ngModule) {
|
||||
throw new Error('Cannot set base providers because it has already been called');
|
||||
}
|
||||
this.platform = platform;
|
||||
this.ngModule = ngModule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the providers for the test injector.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
resetTestEnvironment() {
|
||||
this.resetTestingModule();
|
||||
this.platform = null;
|
||||
this.ngModule = null;
|
||||
}
|
||||
|
||||
resetTestingModule() {
|
||||
this._compiler = null;
|
||||
this._moduleOverrides = [];
|
||||
this._componentOverrides = [];
|
||||
this._directiveOverrides = [];
|
||||
this._pipeOverrides = [];
|
||||
|
||||
this._moduleRef = null;
|
||||
this._moduleWithComponentFactories = null;
|
||||
this._compilerOptions = [];
|
||||
this._providers = [];
|
||||
this._declarations = [];
|
||||
this._imports = [];
|
||||
this._schemas = [];
|
||||
this._instantiated = false;
|
||||
this._activeFixtures.forEach((fixture) => fixture.destroy());
|
||||
this._activeFixtures = [];
|
||||
}
|
||||
|
||||
platform: PlatformRef = null;
|
||||
|
||||
ngModule: Type<any> = null;
|
||||
|
||||
configureCompiler(config: {providers?: any[], useJit?: boolean}) {
|
||||
this._assertNotInstantiated('TestBed.configureCompiler', 'configure the compiler');
|
||||
this._compilerOptions.push(config);
|
||||
}
|
||||
|
||||
configureTestingModule(moduleDef: TestModuleMetadata) {
|
||||
this._assertNotInstantiated('TestBed.configureTestingModule', 'configure the test module');
|
||||
if (moduleDef.providers) {
|
||||
this._providers.push(...moduleDef.providers);
|
||||
}
|
||||
if (moduleDef.declarations) {
|
||||
this._declarations.push(...moduleDef.declarations);
|
||||
}
|
||||
if (moduleDef.imports) {
|
||||
this._imports.push(...moduleDef.imports);
|
||||
}
|
||||
if (moduleDef.schemas) {
|
||||
this._schemas.push(...moduleDef.schemas);
|
||||
}
|
||||
}
|
||||
|
||||
compileComponents(): Promise<any> {
|
||||
if (this._moduleWithComponentFactories || this._instantiated) {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
|
||||
const moduleType = this._createCompilerAndModule();
|
||||
return this._compiler.compileModuleAndAllComponentsAsync(moduleType)
|
||||
.then((moduleAndComponentFactories) => {
|
||||
this._moduleWithComponentFactories = moduleAndComponentFactories;
|
||||
});
|
||||
}
|
||||
|
||||
private _initIfNeeded() {
|
||||
if (this._instantiated) {
|
||||
return;
|
||||
}
|
||||
if (!this._moduleWithComponentFactories) {
|
||||
try {
|
||||
const moduleType = this._createCompilerAndModule();
|
||||
this._moduleWithComponentFactories =
|
||||
this._compiler.compileModuleAndAllComponentsSync(moduleType);
|
||||
} catch (e) {
|
||||
if (getComponentType(e)) {
|
||||
throw new Error(
|
||||
`This test module uses the component ${stringify(getComponentType(e))} which is using a "templateUrl" or "styleUrls", but they were never compiled. ` +
|
||||
`Please call "TestBed.compileComponents" before your test.`);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
const ngZone = new NgZone({enableLongStackTrace: true});
|
||||
const ngZoneInjector = ReflectiveInjector.resolveAndCreate(
|
||||
[{provide: NgZone, useValue: ngZone}], this.platform.injector);
|
||||
this._moduleRef = this._moduleWithComponentFactories.ngModuleFactory.create(ngZoneInjector);
|
||||
this._instantiated = true;
|
||||
}
|
||||
|
||||
private _createCompilerAndModule(): Type<any> {
|
||||
const providers = this._providers.concat([{provide: TestBed, useValue: this}]);
|
||||
const declarations = this._declarations;
|
||||
const imports = [this.ngModule, this._imports];
|
||||
const schemas = this._schemas;
|
||||
|
||||
@NgModule(
|
||||
{providers: providers, declarations: declarations, imports: imports, schemas: schemas})
|
||||
class DynamicTestModule {
|
||||
}
|
||||
|
||||
const compilerFactory: TestingCompilerFactory =
|
||||
this.platform.injector.get(TestingCompilerFactory);
|
||||
this._compiler =
|
||||
compilerFactory.createTestingCompiler(this._compilerOptions.concat([{useDebug: true}]));
|
||||
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]));
|
||||
return DynamicTestModule;
|
||||
}
|
||||
|
||||
private _assertNotInstantiated(methodName: string, methodDescription: string) {
|
||||
if (this._instantiated) {
|
||||
throw new Error(
|
||||
`Cannot ${methodDescription} when the test module has already been instantiated. ` +
|
||||
`Make sure you are not using \`inject\` before \`${methodName}\`.`);
|
||||
}
|
||||
}
|
||||
|
||||
get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) {
|
||||
this._initIfNeeded();
|
||||
if (token === TestBed) {
|
||||
return this;
|
||||
}
|
||||
// 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.
|
||||
const result = this._moduleRef.injector.get(token, UNDEFINED);
|
||||
return result === UNDEFINED ? this._compiler.injector.get(token, notFoundValue) : result;
|
||||
}
|
||||
|
||||
execute(tokens: any[], fn: Function, context?: any): any {
|
||||
this._initIfNeeded();
|
||||
const params = tokens.map(t => this.get(t));
|
||||
return fn.apply(context, params);
|
||||
}
|
||||
|
||||
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void {
|
||||
this._assertNotInstantiated('overrideModule', 'override module metadata');
|
||||
this._moduleOverrides.push([ngModule, override]);
|
||||
}
|
||||
|
||||
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void {
|
||||
this._assertNotInstantiated('overrideComponent', 'override component metadata');
|
||||
this._componentOverrides.push([component, override]);
|
||||
}
|
||||
|
||||
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void {
|
||||
this._assertNotInstantiated('overrideDirective', 'override directive metadata');
|
||||
this._directiveOverrides.push([directive, override]);
|
||||
}
|
||||
|
||||
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void {
|
||||
this._assertNotInstantiated('overridePipe', 'override pipe metadata');
|
||||
this._pipeOverrides.push([pipe, override]);
|
||||
}
|
||||
|
||||
createComponent<T>(component: Type<T>): ComponentFixture<T> {
|
||||
this._initIfNeeded();
|
||||
const componentFactory = this._moduleWithComponentFactories.componentFactories.find(
|
||||
(compFactory) => compFactory.componentType === component);
|
||||
if (!componentFactory) {
|
||||
throw new Error(
|
||||
`Cannot create the component ${stringify(component)} as it was not imported into the testing module!`);
|
||||
}
|
||||
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 = () => {
|
||||
const componentRef = componentFactory.create(this, [], `#${rootElId}`);
|
||||
return new ComponentFixture<T>(componentRef, ngZone, autoDetect);
|
||||
};
|
||||
|
||||
const fixture = !ngZone ? initComponent() : ngZone.run(initComponent);
|
||||
this._activeFixtures.push(fixture);
|
||||
return fixture;
|
||||
}
|
||||
}
|
||||
|
||||
let _testBed: TestBed = null;
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export function getTestBed() {
|
||||
return _testBed = _testBed || new TestBed();
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows injecting dependencies in `beforeEach()` and `it()`.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```
|
||||
* beforeEach(inject([Dependency, AClass], (dep, object) => {
|
||||
* // some code that uses `dep` and `object`
|
||||
* // ...
|
||||
* }));
|
||||
*
|
||||
* it('...', inject([AClass], (object) => {
|
||||
* object.doSomething();
|
||||
* expect(...);
|
||||
* })
|
||||
* ```
|
||||
*
|
||||
* Notes:
|
||||
* - inject is currently a function because of some Traceur limitation the syntax should
|
||||
* eventually
|
||||
* becomes `it('...', @Inject (object: AClass, async: AsyncTestCompleter) => { ... });`
|
||||
*
|
||||
* @stable
|
||||
*/
|
||||
export function inject(tokens: any[], fn: Function): () => any {
|
||||
const testBed = getTestBed();
|
||||
if (tokens.indexOf(AsyncTestCompleter) >= 0) {
|
||||
// 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;
|
||||
});
|
||||
};
|
||||
} else {
|
||||
// Not using an arrow function to preserve context passed from call site
|
||||
return function() { return testBed.execute(tokens, fn, this); };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export class InjectSetupWrapper {
|
||||
constructor(private _moduleDef: () => TestModuleMetadata) {}
|
||||
|
||||
private _addModule() {
|
||||
const moduleDef = this._moduleDef();
|
||||
if (moduleDef) {
|
||||
getTestBed().configureTestingModule(moduleDef);
|
||||
}
|
||||
}
|
||||
|
||||
inject(tokens: any[], fn: Function): () => any {
|
||||
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);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export function withModule(moduleDef: TestModuleMetadata): InjectSetupWrapper;
|
||||
export function withModule(moduleDef: TestModuleMetadata, fn: Function): () => any;
|
||||
export function withModule(moduleDef: TestModuleMetadata, fn: Function = null): (() => any)|
|
||||
InjectSetupWrapper {
|
||||
if (fn) {
|
||||
// Not using an arrow function to preserve context passed from call site
|
||||
return function() {
|
||||
const testBed = getTestBed();
|
||||
if (moduleDef) {
|
||||
testBed.configureTestingModule(moduleDef);
|
||||
}
|
||||
return fn.apply(this);
|
||||
};
|
||||
}
|
||||
return new InjectSetupWrapper(() => moduleDef);
|
||||
}
|
||||
|
||||
function getComponentType(error: Error): Function {
|
||||
return (error as any)[ɵERROR_COMPONENT_TYPE];
|
||||
}
|
45
packages/core/testing/src/test_compiler.ts
Normal file
45
packages/core/testing/src/test_compiler.ts
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {Compiler, CompilerOptions, Component, Directive, Injector, NgModule, Pipe, Type} from '@angular/core';
|
||||
|
||||
import {MetadataOverride} from './metadata_override';
|
||||
|
||||
function unimplemented(): any {
|
||||
throw Error('unimplemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* Special interface to the compiler only used by testing
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class TestingCompiler extends Compiler {
|
||||
get injector(): Injector { throw unimplemented(); }
|
||||
overrideModule(module: Type<any>, overrides: MetadataOverride<NgModule>): void {
|
||||
throw unimplemented();
|
||||
}
|
||||
overrideDirective(directive: Type<any>, overrides: MetadataOverride<Directive>): void {
|
||||
throw unimplemented();
|
||||
}
|
||||
overrideComponent(component: Type<any>, overrides: MetadataOverride<Component>): void {
|
||||
throw unimplemented();
|
||||
}
|
||||
overridePipe(directive: Type<any>, overrides: MetadataOverride<Pipe>): void {
|
||||
throw unimplemented();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for creating a Compiler
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export abstract class TestingCompilerFactory {
|
||||
abstract createTestingCompiler(options?: CompilerOptions[]): TestingCompiler;
|
||||
}
|
203
packages/core/testing/src/testing_internal.ts
Normal file
203
packages/core/testing/src/testing_internal.ts
Normal file
@ -0,0 +1,203 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {ɵisPromise as isPromise, ɵmerge as merge} from '@angular/core';
|
||||
import {global} from '@angular/core/src/util';
|
||||
|
||||
import {AsyncTestCompleter} from './async_test_completer';
|
||||
import {getTestBed, inject} from './test_bed';
|
||||
|
||||
export {AsyncTestCompleter} from './async_test_completer';
|
||||
export {inject} from './test_bed';
|
||||
|
||||
export * from './logger';
|
||||
export * from './ng_zone_mock';
|
||||
|
||||
export const proxy: ClassDecorator = (t: any) => t;
|
||||
|
||||
const _global = <any>(typeof window === 'undefined' ? global : window);
|
||||
|
||||
export const afterEach: Function = _global.afterEach;
|
||||
export const expect: (actual: any) => jasmine.Matchers = _global.expect;
|
||||
|
||||
const jsmBeforeEach = _global.beforeEach;
|
||||
const jsmDescribe = _global.describe;
|
||||
const jsmDDescribe = _global.fdescribe;
|
||||
const jsmXDescribe = _global.xdescribe;
|
||||
const jsmIt = _global.it;
|
||||
const jsmIIt = _global.fit;
|
||||
const jsmXIt = _global.xit;
|
||||
|
||||
const runnerStack: BeforeEachRunner[] = [];
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 3000;
|
||||
const globalTimeOut = jasmine.DEFAULT_TIMEOUT_INTERVAL;
|
||||
|
||||
const testBed = getTestBed();
|
||||
|
||||
/**
|
||||
* Mechanism to run `beforeEach()` functions of Angular tests.
|
||||
*
|
||||
* Note: Jasmine own `beforeEach` is used by this library to handle DI providers.
|
||||
*/
|
||||
class BeforeEachRunner {
|
||||
private _fns: Array<Function> = [];
|
||||
|
||||
constructor(private _parent: BeforeEachRunner) {}
|
||||
|
||||
beforeEach(fn: Function): void { this._fns.push(fn); }
|
||||
|
||||
run(): void {
|
||||
if (this._parent) this._parent.run();
|
||||
this._fns.forEach((fn) => { fn(); });
|
||||
}
|
||||
}
|
||||
|
||||
// Reset the test providers before each test
|
||||
jsmBeforeEach(() => { testBed.resetTestingModule(); });
|
||||
|
||||
function _describe(jsmFn: Function, ...args: any[]) {
|
||||
const parentRunner = runnerStack.length === 0 ? null : runnerStack[runnerStack.length - 1];
|
||||
const runner = new BeforeEachRunner(parentRunner);
|
||||
runnerStack.push(runner);
|
||||
const suite = jsmFn(...args);
|
||||
runnerStack.pop();
|
||||
return suite;
|
||||
}
|
||||
|
||||
export function describe(...args: any[]): void {
|
||||
return _describe(jsmDescribe, ...args);
|
||||
}
|
||||
|
||||
export function ddescribe(...args: any[]): void {
|
||||
return _describe(jsmDDescribe, ...args);
|
||||
}
|
||||
|
||||
export function xdescribe(...args: any[]): void {
|
||||
return _describe(jsmXDescribe, ...args);
|
||||
}
|
||||
|
||||
export function beforeEach(fn: Function): void {
|
||||
if (runnerStack.length > 0) {
|
||||
// Inside a describe block, beforeEach() uses a BeforeEachRunner
|
||||
runnerStack[runnerStack.length - 1].beforeEach(fn);
|
||||
} else {
|
||||
// Top level beforeEach() are delegated to jasmine
|
||||
jsmBeforeEach(fn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows overriding default providers defined in test_injector.js.
|
||||
*
|
||||
* The given function must return a list of DI providers.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* beforeEachProviders(() => [
|
||||
* {provide: Compiler, useClass: MockCompiler},
|
||||
* {provide: SomeToken, useValue: myValue},
|
||||
* ]);
|
||||
*/
|
||||
export function beforeEachProviders(fn: Function): void {
|
||||
jsmBeforeEach(() => {
|
||||
const providers = fn();
|
||||
if (!providers) return;
|
||||
testBed.configureTestingModule({providers: providers});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: number): void {
|
||||
if (runnerStack.length == 0) {
|
||||
// This left here intentionally, as we should never get here, and it aids debugging.
|
||||
debugger;
|
||||
throw new Error('Empty Stack!');
|
||||
}
|
||||
const runner = runnerStack[runnerStack.length - 1];
|
||||
const timeOut = Math.max(globalTimeOut, testTimeOut);
|
||||
|
||||
jsmFn(name, (done: any) => {
|
||||
const completerProvider = {
|
||||
provide: AsyncTestCompleter,
|
||||
useFactory: () => {
|
||||
// Mark the test as async when an AsyncTestCompleter is injected in an it()
|
||||
return new AsyncTestCompleter();
|
||||
}
|
||||
};
|
||||
testBed.configureTestingModule({providers: [completerProvider]});
|
||||
runner.run();
|
||||
|
||||
if (testFn.length == 0) {
|
||||
const retVal = testFn();
|
||||
if (isPromise(retVal)) {
|
||||
// Asynchronous test function that returns a Promise - wait for completion.
|
||||
(<Promise<any>>retVal).then(done, done.fail);
|
||||
} else {
|
||||
// Synchronous test function - complete immediately.
|
||||
done();
|
||||
}
|
||||
} else {
|
||||
// Asynchronous test function that takes in 'done' parameter.
|
||||
testFn(done);
|
||||
}
|
||||
}, timeOut);
|
||||
}
|
||||
|
||||
export function it(name: any, fn: any, timeOut: any = null): void {
|
||||
return _it(jsmIt, name, fn, timeOut);
|
||||
}
|
||||
|
||||
export function xit(name: any, fn: any, timeOut: any = null): void {
|
||||
return _it(jsmXIt, name, fn, timeOut);
|
||||
}
|
||||
|
||||
export function iit(name: any, fn: any, timeOut: any = null): void {
|
||||
return _it(jsmIIt, name, fn, timeOut);
|
||||
}
|
||||
|
||||
export class SpyObject {
|
||||
constructor(type?: any) {
|
||||
if (type) {
|
||||
for (const prop in type.prototype) {
|
||||
let m: any = null;
|
||||
try {
|
||||
m = type.prototype[prop];
|
||||
} catch (e) {
|
||||
// As we are creating spys for abstract classes,
|
||||
// these classes might have getters that throw when they are accessed.
|
||||
// As we are only auto creating spys for methods, this
|
||||
// should not matter.
|
||||
}
|
||||
if (typeof m === 'function') {
|
||||
this.spy(prop);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spy(name: string) {
|
||||
if (!(this as any)[name]) {
|
||||
(this as any)[name] = jasmine.createSpy(name);
|
||||
}
|
||||
return (this as any)[name];
|
||||
}
|
||||
|
||||
prop(name: string, value: any) { (this as any)[name] = value; }
|
||||
|
||||
static stub(object: any = null, config: any = null, overrides: any = null) {
|
||||
if (!(object instanceof SpyObject)) {
|
||||
overrides = config;
|
||||
config = object;
|
||||
object = new SpyObject();
|
||||
}
|
||||
|
||||
const m = merge(config, overrides);
|
||||
Object.keys(m).forEach(key => { object.spy(key).and.returnValue(m[key]); });
|
||||
return object;
|
||||
}
|
||||
}
|
18
packages/core/testing/tsconfig-build.json
Normal file
18
packages/core/testing/tsconfig-build.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"extends": "./tsconfig-build",
|
||||
|
||||
"compilerOptions": {
|
||||
"paths": {
|
||||
"rxjs/*": ["../../../node_modules/rxjs/*"],
|
||||
"@angular/core": ["../../../dist/packages-dist/core"]
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"testing/index.ts",
|
||||
"../../../node_modules/zone.js/dist/zone.js.d.ts",
|
||||
"../../system.d.ts"
|
||||
],
|
||||
"angularCompilerOptions": {
|
||||
"strictMetadataEmit": true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user