2015-10-08 18:33:17 -04:00
|
|
|
/**
|
|
|
|
* Public Test Library for unit testing Angular2 Applications. Uses the
|
|
|
|
* Jasmine framework.
|
|
|
|
*/
|
2015-11-06 20:34:07 -05:00
|
|
|
import {global} from 'angular2/src/facade/lang';
|
2015-11-18 21:46:24 -05:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
2015-12-04 08:29:39 -05:00
|
|
|
import {bind} from 'angular2/core';
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
FunctionWithParamTokens,
|
|
|
|
inject,
|
|
|
|
injectAsync,
|
|
|
|
TestInjector,
|
|
|
|
getTestInjector
|
|
|
|
} from './test_injector';
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
export {inject, injectAsync} from './test_injector';
|
|
|
|
|
2015-10-13 03:29:13 -04:00
|
|
|
export {expect, NgMatchers} from './matchers';
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-02-01 13:28:57 -05:00
|
|
|
var _global = <any>(typeof window === 'undefined' ? global : window);
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2015-11-18 21:46:24 -05:00
|
|
|
/**
|
2015-11-30 11:28:54 -05:00
|
|
|
* Run a function (with an optional asynchronous callback) after each test case.
|
|
|
|
*
|
|
|
|
* See http://jasmine.github.io/ for more details.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {@example testing/ts/testing.ts region='afterEach'}
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export var afterEach: Function = _global.afterEach;
|
2015-11-18 21:46:24 -05:00
|
|
|
|
|
|
|
/**
|
2015-11-30 11:28:54 -05:00
|
|
|
* Group test cases together under a common description prefix.
|
|
|
|
*
|
|
|
|
* See http://jasmine.github.io/ for more details.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {@example testing/ts/testing.ts region='describeIt'}
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export var describe: Function = _global.describe;
|
2015-11-18 21:46:24 -05:00
|
|
|
|
|
|
|
/**
|
2015-11-30 11:28:54 -05:00
|
|
|
* See {@link fdescribe}.
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export var ddescribe: Function = _global.fdescribe;
|
2015-11-18 21:46:24 -05:00
|
|
|
|
|
|
|
/**
|
2015-11-30 11:28:54 -05:00
|
|
|
* Like {@link describe}, but instructs the test runner to only run
|
|
|
|
* the test cases in this group. This is useful for debugging.
|
|
|
|
*
|
|
|
|
* See http://jasmine.github.io/ for more details.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {@example testing/ts/testing.ts region='fdescribe'}
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export var fdescribe: Function = _global.fdescribe;
|
2015-11-18 21:46:24 -05:00
|
|
|
|
|
|
|
/**
|
2015-11-30 11:28:54 -05:00
|
|
|
* Like {@link describe}, but instructs the test runner to exclude
|
|
|
|
* this group of test cases from execution. This is useful for
|
|
|
|
* debugging, or for excluding broken tests until they can be fixed.
|
|
|
|
*
|
|
|
|
* See http://jasmine.github.io/ for more details.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {@example testing/ts/testing.ts region='xdescribe'}
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export var xdescribe: Function = _global.xdescribe;
|
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* Signature for a synchronous test function (no arguments).
|
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export type SyncTestFn = () => void;
|
2015-12-03 18:49:09 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signature for an asynchronous test function which takes a
|
|
|
|
* `done` callback.
|
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export type AsyncTestFn = (done: () => void) => void;
|
2015-12-03 18:49:09 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signature for any simple testing function.
|
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export type AnyTestFn = SyncTestFn | AsyncTestFn;
|
|
|
|
|
|
|
|
var jsmBeforeEach = _global.beforeEach;
|
|
|
|
var jsmIt = _global.it;
|
|
|
|
var jsmIIt = _global.fit;
|
|
|
|
var jsmXIt = _global.xit;
|
|
|
|
|
2015-12-08 22:03:21 -05:00
|
|
|
var testInjector: TestInjector = getTestInjector();
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
// Reset the test providers before each test.
|
2015-12-08 22:03:21 -05:00
|
|
|
jsmBeforeEach(() => { testInjector.reset(); });
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows overriding default providers of the test injector,
|
2015-11-18 21:46:24 -05:00
|
|
|
* which are defined in test_injector.js.
|
2015-10-08 18:33:17 -04:00
|
|
|
*
|
|
|
|
* The given function must return a list of DI providers.
|
|
|
|
*
|
2015-11-30 11:28:54 -05:00
|
|
|
* ## Example:
|
2015-10-08 18:33:17 -04:00
|
|
|
*
|
2015-11-30 11:28:54 -05:00
|
|
|
* {@example testing/ts/testing.ts region='beforeEachProviders'}
|
2015-10-08 18:33:17 -04:00
|
|
|
*/
|
|
|
|
export function beforeEachProviders(fn): void {
|
|
|
|
jsmBeforeEach(() => {
|
|
|
|
var providers = fn();
|
|
|
|
if (!providers) return;
|
2015-12-08 22:03:21 -05:00
|
|
|
try {
|
|
|
|
testInjector.addProviders(providers);
|
|
|
|
} catch (e) {
|
2016-04-12 12:40:37 -04:00
|
|
|
throw new Error('beforeEachProviders was called after the injector had ' +
|
|
|
|
'been used in a beforeEach or it block. This invalidates the ' +
|
|
|
|
'test injector');
|
2015-10-08 18:33:17 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _isPromiseLike(input): boolean {
|
|
|
|
return input && !!(input.then);
|
|
|
|
}
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | AnyTestFn,
|
|
|
|
testTimeOut: number): void {
|
2015-10-08 18:33:17 -04:00
|
|
|
var timeOut = testTimeOut;
|
|
|
|
|
|
|
|
if (testFn instanceof FunctionWithParamTokens) {
|
2016-04-07 16:29:52 -04:00
|
|
|
let testFnT = testFn;
|
2015-11-18 21:46:24 -05:00
|
|
|
jsmFn(name, (done) => {
|
2016-01-20 18:55:30 -05:00
|
|
|
var returnedTestValue;
|
|
|
|
try {
|
2016-04-07 16:29:52 -04:00
|
|
|
returnedTestValue = testInjector.execute(testFnT);
|
2016-01-20 18:55:30 -05:00
|
|
|
} catch (err) {
|
|
|
|
done.fail(err);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-08 19:44:04 -05:00
|
|
|
|
2016-04-07 16:29:52 -04:00
|
|
|
if (testFnT.isAsync) {
|
2015-12-08 19:44:04 -05:00
|
|
|
if (_isPromiseLike(returnedTestValue)) {
|
2016-01-20 18:55:30 -05:00
|
|
|
(<Promise<any>>returnedTestValue).then(() => { done(); }, (err) => { done.fail(err); });
|
2015-12-08 19:44:04 -05:00
|
|
|
} else {
|
2016-04-12 12:40:37 -04:00
|
|
|
done.fail('Error: injectAsync was expected to return a promise, but the ' +
|
|
|
|
' returned value was: ' + returnedTestValue);
|
2015-12-08 19:44:04 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(returnedTestValue === undefined)) {
|
2016-04-12 12:40:37 -04:00
|
|
|
done.fail('Error: inject returned a value. Did you mean to use injectAsync? Returned ' +
|
|
|
|
'value was: ' + returnedTestValue);
|
2015-12-08 19:44:04 -05:00
|
|
|
}
|
2016-01-20 18:55:30 -05:00
|
|
|
done();
|
2015-11-18 21:46:24 -05:00
|
|
|
}
|
|
|
|
}, timeOut);
|
2015-10-08 18:33:17 -04:00
|
|
|
} else {
|
|
|
|
// The test case doesn't use inject(). ie `it('test', (done) => { ... }));`
|
|
|
|
jsmFn(name, testFn, timeOut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:46:24 -05:00
|
|
|
/**
|
|
|
|
* Wrapper around Jasmine beforeEach function.
|
|
|
|
*
|
|
|
|
* beforeEach may be used with the `inject` function to fetch dependencies.
|
|
|
|
* The test will automatically wait for any asynchronous calls inside the
|
|
|
|
* injected test function to complete.
|
2015-11-30 11:28:54 -05:00
|
|
|
*
|
|
|
|
* See http://jasmine.github.io/ for more details.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {@example testing/ts/testing.ts region='beforeEach'}
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export function beforeEach(fn: FunctionWithParamTokens | AnyTestFn): void {
|
|
|
|
if (fn instanceof FunctionWithParamTokens) {
|
|
|
|
// The test case uses inject(). ie `beforeEach(inject([ClassA], (a) => { ...
|
|
|
|
// }));`
|
2016-04-07 16:29:52 -04:00
|
|
|
let fnT = fn;
|
2015-12-14 23:27:31 -05:00
|
|
|
jsmBeforeEach((done) => {
|
|
|
|
|
2016-01-20 18:55:30 -05:00
|
|
|
var returnedTestValue;
|
|
|
|
try {
|
2016-04-07 16:29:52 -04:00
|
|
|
returnedTestValue = testInjector.execute(fnT);
|
2016-01-20 18:55:30 -05:00
|
|
|
} catch (err) {
|
|
|
|
done.fail(err);
|
|
|
|
return;
|
|
|
|
}
|
2016-04-07 16:29:52 -04:00
|
|
|
if (fnT.isAsync) {
|
2015-12-08 19:44:04 -05:00
|
|
|
if (_isPromiseLike(returnedTestValue)) {
|
2016-01-20 18:55:30 -05:00
|
|
|
(<Promise<any>>returnedTestValue).then(() => { done(); }, (err) => { done.fail(err); });
|
2015-12-08 19:44:04 -05:00
|
|
|
} else {
|
2016-04-12 12:40:37 -04:00
|
|
|
done.fail('Error: injectAsync was expected to return a promise, but the ' +
|
|
|
|
' returned value was: ' + returnedTestValue);
|
2015-12-08 19:44:04 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(returnedTestValue === undefined)) {
|
2016-04-12 12:40:37 -04:00
|
|
|
done.fail('Error: inject returned a value. Did you mean to use injectAsync? Returned ' +
|
|
|
|
'value was: ' + returnedTestValue);
|
2015-12-08 19:44:04 -05:00
|
|
|
}
|
2016-01-20 18:55:30 -05:00
|
|
|
done();
|
2015-12-08 19:44:04 -05:00
|
|
|
}
|
2015-12-14 23:27:31 -05:00
|
|
|
});
|
2015-10-08 18:33:17 -04:00
|
|
|
} else {
|
|
|
|
// The test case doesn't use inject(). ie `beforeEach((done) => { ... }));`
|
|
|
|
if ((<any>fn).length === 0) {
|
|
|
|
jsmBeforeEach(() => { (<SyncTestFn>fn)(); });
|
|
|
|
} else {
|
|
|
|
jsmBeforeEach((done) => { (<AsyncTestFn>fn)(done); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:46:24 -05:00
|
|
|
/**
|
2015-11-30 11:28:54 -05:00
|
|
|
* Define a single test case with the given test name and execution function.
|
2015-11-18 21:46:24 -05:00
|
|
|
*
|
2015-11-30 11:28:54 -05:00
|
|
|
* The test function can be either a synchronous function, an asynchronous function
|
|
|
|
* that takes a completion callback, or an injected function created via {@link inject}
|
|
|
|
* or {@link injectAsync}. The test will automatically wait for any asynchronous calls
|
|
|
|
* inside the injected test function to complete.
|
|
|
|
*
|
|
|
|
* Wrapper around Jasmine it function. See http://jasmine.github.io/ for more details.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
2015-12-03 18:49:09 -05:00
|
|
|
* {@example testing/ts/testing.ts region='describeIt'}
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2016-04-12 12:40:37 -04:00
|
|
|
export function it(name: string, fn: FunctionWithParamTokens | AnyTestFn,
|
|
|
|
timeOut: number = null): void {
|
2015-10-08 18:33:17 -04:00
|
|
|
return _it(jsmIt, name, fn, timeOut);
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:46:24 -05:00
|
|
|
/**
|
2015-11-30 11:28:54 -05:00
|
|
|
* Like {@link it}, but instructs the test runner to exclude this test
|
|
|
|
* entirely. Useful for debugging or for excluding broken tests until
|
|
|
|
* they can be fixed.
|
2015-11-18 21:46:24 -05:00
|
|
|
*
|
2015-11-30 11:28:54 -05:00
|
|
|
* Wrapper around Jasmine xit function. See http://jasmine.github.io/ for more details.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {@example testing/ts/testing.ts region='xit'}
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2016-04-12 12:40:37 -04:00
|
|
|
export function xit(name: string, fn: FunctionWithParamTokens | AnyTestFn,
|
|
|
|
timeOut: number = null): void {
|
2015-10-08 18:33:17 -04:00
|
|
|
return _it(jsmXIt, name, fn, timeOut);
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:46:24 -05:00
|
|
|
/**
|
2015-11-30 11:28:54 -05:00
|
|
|
* See {@link fit}.
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2016-04-12 12:40:37 -04:00
|
|
|
export function iit(name: string, fn: FunctionWithParamTokens | AnyTestFn,
|
|
|
|
timeOut: number = null): void {
|
2015-10-08 18:33:17 -04:00
|
|
|
return _it(jsmIIt, name, fn, timeOut);
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:46:24 -05:00
|
|
|
/**
|
2015-11-30 11:28:54 -05:00
|
|
|
* Like {@link it}, but instructs the test runner to only run this test.
|
|
|
|
* Useful for debugging.
|
2015-11-18 21:46:24 -05:00
|
|
|
*
|
2015-11-30 11:28:54 -05:00
|
|
|
* Wrapper around Jasmine fit function. See http://jasmine.github.io/ for more details.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {@example testing/ts/testing.ts region='fit'}
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2016-04-12 12:40:37 -04:00
|
|
|
export function fit(name: string, fn: FunctionWithParamTokens | AnyTestFn,
|
|
|
|
timeOut: number = null): void {
|
2015-10-08 18:33:17 -04:00
|
|
|
return _it(jsmIIt, name, fn, timeOut);
|
|
|
|
}
|