forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.ts
More file actions
245 lines (221 loc) · 6.79 KB
/
testing.ts
File metadata and controls
245 lines (221 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* Public Test Library for unit testing Angular2 Applications. Uses the
* Jasmine framework.
*/
import {global} from 'angular2/src/facade/lang';
import {ListWrapper} from 'angular2/src/facade/collection';
import {bind} from 'angular2/core';
import {
FunctionWithParamTokens,
inject,
async,
injectAsync,
TestInjector,
getTestInjector
} from './test_injector';
export {inject, async, injectAsync} from './test_injector';
export {expect, NgMatchers} from './matchers';
var _global = <any>(typeof window === 'undefined' ? global : window);
/**
* 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'}
*/
export var afterEach: Function = _global.afterEach;
/**
* 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'}
*/
export var describe: Function = _global.describe;
/**
* See {@link fdescribe}.
*/
export var ddescribe: Function = _global.fdescribe;
/**
* 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'}
*/
export var fdescribe: Function = _global.fdescribe;
/**
* 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'}
*/
export var xdescribe: Function = _global.xdescribe;
/**
* Signature for a synchronous test function (no arguments).
*/
export type SyncTestFn = () => void;
/**
* Signature for an asynchronous test function which takes a
* `done` callback.
*/
export type AsyncTestFn = (done: () => void) => void;
/**
* Signature for any simple testing function.
*/
export type AnyTestFn = SyncTestFn | AsyncTestFn | Function;
var jsmBeforeEach = _global.beforeEach;
var jsmIt = _global.it;
var jsmIIt = _global.fit;
var jsmXIt = _global.xit;
var testInjector: TestInjector = getTestInjector();
// Reset the test providers before each test.
jsmBeforeEach(() => { testInjector.reset(); });
/**
* Allows overriding default providers of the test injector,
* which are defined in test_injector.js.
*
* The given function must return a list of DI providers.
*
* ## Example:
*
* {@example testing/ts/testing.ts region='beforeEachProviders'}
*/
export function beforeEachProviders(fn): void {
jsmBeforeEach(() => {
var providers = fn();
if (!providers) return;
try {
testInjector.addProviders(providers);
} catch (e) {
throw new Error('beforeEachProviders was called after the injector had ' +
'been used in a beforeEach or it block. This invalidates the ' +
'test injector');
}
});
}
function runInAsyncTestZone(fnToExecute, finishCallback: Function, failCallback: Function,
testName = ''): any {
var AsyncTestZoneSpec = Zone['AsyncTestZoneSpec'];
var testZoneSpec = new AsyncTestZoneSpec(finishCallback, failCallback, testName);
var testZone = Zone.current.fork(testZoneSpec);
return testZone.run(fnToExecute);
}
function _isPromiseLike(input): boolean {
return input && !!(input.then);
}
function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | AnyTestFn,
testTimeOut: number): void {
var timeOut = testTimeOut;
if (testFn instanceof FunctionWithParamTokens) {
let testFnT = testFn;
jsmFn(name, (done) => {
if (testFnT.isAsync) {
runInAsyncTestZone(() => testInjector.execute(testFnT), done, done.fail, name);
} else {
testInjector.execute(testFnT);
done();
}
}, timeOut);
} else {
// The test case doesn't use inject(). ie `it('test', (done) => { ... }));`
jsmFn(name, testFn, timeOut);
}
}
/**
* Wrapper around Jasmine beforeEach function.
*
* beforeEach may be used with the `inject` function to fetch dependencies.
*
* See http://jasmine.github.io/ for more details.
*
* ## Example:
*
* {@example testing/ts/testing.ts region='beforeEach'}
*/
export function beforeEach(fn: FunctionWithParamTokens | AnyTestFn): void {
if (fn instanceof FunctionWithParamTokens) {
// The test case uses inject(). ie `beforeEach(inject([ClassA], (a) => { ...
// }));`
let fnT = fn;
jsmBeforeEach((done) => {
if (fnT.isAsync) {
runInAsyncTestZone(() => testInjector.execute(fnT), done, done.fail, 'beforeEach');
} else {
testInjector.execute(fnT);
done();
}
});
} 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); });
}
}
}
/**
* Define a single test case with the given test name and execution function.
*
* The test function can be either a synchronous function, the result of {@link async},
* or an injected function created via {@link inject}.
*
* Wrapper around Jasmine it function. See http://jasmine.github.io/ for more details.
*
* ## Example:
*
* {@example testing/ts/testing.ts region='describeIt'}
*/
export function it(name: string, fn: FunctionWithParamTokens | AnyTestFn,
timeOut: number = null): void {
return _it(jsmIt, name, fn, timeOut);
}
/**
* 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.
*
* Wrapper around Jasmine xit function. See http://jasmine.github.io/ for more details.
*
* ## Example:
*
* {@example testing/ts/testing.ts region='xit'}
*/
export function xit(name: string, fn: FunctionWithParamTokens | AnyTestFn,
timeOut: number = null): void {
return _it(jsmXIt, name, fn, timeOut);
}
/**
* See {@link fit}.
*/
export function iit(name: string, fn: FunctionWithParamTokens | AnyTestFn,
timeOut: number = null): void {
return _it(jsmIIt, name, fn, timeOut);
}
/**
* Like {@link it}, but instructs the test runner to only run this test.
* Useful for debugging.
*
* Wrapper around Jasmine fit function. See http://jasmine.github.io/ for more details.
*
* ## Example:
*
* {@example testing/ts/testing.ts region='fit'}
*/
export function fit(name: string, fn: FunctionWithParamTokens | AnyTestFn,
timeOut: number = null): void {
return _it(jsmIIt, name, fn, timeOut);
}