UNPKG

4.15 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8var _global = (typeof window === 'undefined' ? global : window);
9/**
10 * Wraps a test function in an asynchronous test zone. The test will automatically
11 * complete when all asynchronous calls within this zone are done. Can be used
12 * to wrap an {@link inject} call.
13 *
14 * Example:
15 *
16 * ```
17 * it('...', async(inject([AClass], (object) => {
18 * object.doSomething.then(() => {
19 * expect(...);
20 * })
21 * });
22 * ```
23 *
24 * @stable
25 */
26export function async(fn) {
27 // If we're running using the Jasmine test framework, adapt to call the 'done'
28 // function when asynchronous activity is finished.
29 if (_global.jasmine) {
30 // Not using an arrow function to preserve context passed from call site
31 return function (done) {
32 if (!done) {
33 // if we run beforeEach in @angular/core/testing/testing_internal then we get no done
34 // fake it here and assume sync.
35 done = function () { };
36 done.fail = function (e) { throw e; };
37 }
38 runInTestZone(fn, this, done, function (err) {
39 if (typeof err === 'string') {
40 return done.fail(new Error(err));
41 }
42 else {
43 done.fail(err);
44 }
45 });
46 };
47 }
48 // Otherwise, return a promise which will resolve when asynchronous activity
49 // is finished. This will be correctly consumed by the Mocha framework with
50 // it('...', async(myFn)); or can be used in a custom framework.
51 // Not using an arrow function to preserve context passed from call site
52 return function () {
53 var _this = this;
54 return new Promise(function (finishCallback, failCallback) {
55 runInTestZone(fn, _this, finishCallback, failCallback);
56 });
57 };
58}
59function runInTestZone(fn, context, finishCallback, failCallback) {
60 var currentZone = Zone.current;
61 var AsyncTestZoneSpec = Zone['AsyncTestZoneSpec'];
62 if (AsyncTestZoneSpec === undefined) {
63 throw new Error('AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
64 'Please make sure that your environment includes zone.js/dist/async-test.js');
65 }
66 var ProxyZoneSpec = Zone['ProxyZoneSpec'];
67 if (ProxyZoneSpec === undefined) {
68 throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
69 'Please make sure that your environment includes zone.js/dist/proxy.js');
70 }
71 var proxyZoneSpec = ProxyZoneSpec.get();
72 ProxyZoneSpec.assertPresent();
73 // We need to create the AsyncTestZoneSpec outside the ProxyZone.
74 // If we do it in ProxyZone then we will get to infinite recursion.
75 var proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
76 var previousDelegate = proxyZoneSpec.getDelegate();
77 proxyZone.parent.run(function () {
78 var testZoneSpec = new AsyncTestZoneSpec(function () {
79 // Need to restore the original zone.
80 currentZone.run(function () {
81 if (proxyZoneSpec.getDelegate() == testZoneSpec) {
82 // Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
83 proxyZoneSpec.setDelegate(previousDelegate);
84 }
85 finishCallback();
86 });
87 }, function (error) {
88 // Need to restore the original zone.
89 currentZone.run(function () {
90 if (proxyZoneSpec.getDelegate() == testZoneSpec) {
91 // Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
92 proxyZoneSpec.setDelegate(previousDelegate);
93 }
94 failCallback(error);
95 });
96 }, 'test');
97 proxyZoneSpec.setDelegate(testZoneSpec);
98 });
99 return Zone.current.runGuarded(fn, context);
100}
101//# sourceMappingURL=async.js.map
\No newline at end of file