|
1 | 1 | 'use strict';
|
2 | 2 | (() => {
|
| 3 | + var __extends = function (d, b) { |
| 4 | + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; |
| 5 | + function __() { this.constructor = d; } |
| 6 | + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); |
| 7 | + }; |
3 | 8 | // Patch jasmine's describe/it/beforeEach/afterEach functions so test code always runs
|
4 | 9 | // in a testZone (ProxyZone). (See: angular/zone.js#91 & angular/angular#10503)
|
5 | 10 | if (!Zone) throw new Error("Missing: zone.js");
|
6 |
| - if (typeof jasmine == 'undefined') throw new Error("Missing: jasmine.js"); |
| 11 | + if (typeof jasmine == 'undefined') throw new Error("Missing: jasmine.js"); |
7 | 12 | if (jasmine['__zone_patch__']) throw new Error("'jasmine' has already been patched with 'Zone'.");
|
8 | 13 | jasmine['__zone_patch__'] = true;
|
9 | 14 |
|
|
13 | 18 | if (!ProxyZoneSpec) throw new Error("Missing: ProxyZoneSpec");
|
14 | 19 |
|
15 | 20 | const ambientZone = Zone.current;
|
16 |
| - // Create a synchronous-only zone in which to run `describe` blocks in order to raise an |
17 |
| - // error if any asynchronous operations are attempted inside of a `describe` but outside of |
| 21 | + // Create a synchronous-only zone in which to run `describe` blocks in order to raise an |
| 22 | + // error if any asynchronous operations are attempted inside of a `describe` but outside of |
18 | 23 | // a `beforeEach` or `it`.
|
19 | 24 | const syncZone = ambientZone.fork(new SyncTestZoneSpec('jasmine.describe'));
|
20 | 25 |
|
21 | 26 | // This is the zone which will be used for running individual tests.
|
22 | 27 | // It will be a proxy zone, so that the tests function can retroactively install
|
23 |
| - // different zones. |
| 28 | + // different zones. |
24 | 29 | // Example:
|
25 | 30 | // - In beforeEach() do childZone = Zone.current.fork(...);
|
26 |
| - // - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the |
| 31 | + // - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the |
27 | 32 | // zone outside of fakeAsync it will be able to escope the fakeAsync rules.
|
28 |
| - // - Because ProxyZone is parent fo `childZone` fakeAsync can retroactively add |
| 33 | + // - Because ProxyZone is parent fo `childZone` fakeAsync can retroactively add |
29 | 34 | // fakeAsync behavior to the childZone.
|
30 | 35 | let testProxyZone: Zone = null;
|
31 | 36 |
|
|
35 | 40 | let originalJasmineFn: Function = jasmineEnv[methodName];
|
36 | 41 | jasmineEnv[methodName] = function(description: string, specDefinitions: Function) {
|
37 | 42 | return originalJasmineFn.call(this, description, wrapDescribeInZone(specDefinitions));
|
38 |
| - } |
| 43 | + } |
39 | 44 | });
|
40 | 45 | ['it', 'xit', 'fit'].forEach((methodName) => {
|
41 | 46 | let originalJasmineFn: Function = jasmineEnv[methodName];
|
42 | 47 | jasmineEnv[methodName] = function(description: string, specDefinitions: Function, timeout: number) {
|
43 | 48 | arguments[1] = wrapTestInZone(specDefinitions);
|
44 | 49 | return originalJasmineFn.apply(this, arguments);
|
45 |
| - } |
| 50 | + } |
46 | 51 | });
|
47 | 52 | ['beforeEach', 'afterEach'].forEach((methodName) => {
|
48 | 53 | let originalJasmineFn: Function = jasmineEnv[methodName];
|
49 | 54 | jasmineEnv[methodName] = function(specDefinitions: Function, timeout: number) {
|
50 | 55 | arguments[0] = wrapTestInZone(specDefinitions);
|
51 | 56 | return originalJasmineFn.apply(this, arguments);
|
52 |
| - } |
| 57 | + } |
53 | 58 | });
|
54 | 59 |
|
55 |
| - /** |
56 |
| - * Gets a function wrapping the body of a Jasmine `describe` block to execute in a |
57 |
| - * synchronous-only zone. |
| 60 | + /** |
| 61 | + * Gets a function wrapping the body of a Jasmine `describe` block to execute in a |
| 62 | + * synchronous-only zone. |
58 | 63 | */
|
59 | 64 | function wrapDescribeInZone(describeBody: Function): Function {
|
60 | 65 | return function() {
|
61 | 66 | return syncZone.run(describeBody, this, arguments as any as any[]);
|
62 | 67 | }
|
63 | 68 | }
|
64 | 69 |
|
65 |
| - /** |
66 |
| - * Gets a function wrapping the body of a Jasmine `it/beforeEach/afterEach` block to |
67 |
| - * execute in a ProxyZone zone. |
| 70 | + /** |
| 71 | + * Gets a function wrapping the body of a Jasmine `it/beforeEach/afterEach` block to |
| 72 | + * execute in a ProxyZone zone. |
68 | 73 | * This will run in `testProxyZone`. The `testProxyZone` will be reset by the `ZoneQueueRunner`
|
69 | 74 | */
|
70 | 75 | function wrapTestInZone(testBody: Function): Function {
|
71 | 76 | // The `done` callback is only passed through if the function expects at least one argument.
|
72 | 77 | // Note we have to make a function with correct number of arguments, otherwise jasmine will
|
73 | 78 | // think that all functions are sync or async.
|
74 |
| - return (testBody.length == 0) |
| 79 | + return (testBody.length == 0) |
75 | 80 | ? function() { return testProxyZone.run(testBody, this); }
|
76 | 81 | : function(done) { return testProxyZone.run(testBody, this, [done]); };
|
77 | 82 | }
|
|
90 | 95 | }
|
91 | 96 |
|
92 | 97 | const QueueRunner = (jasmine as any).QueueRunner as { new(attrs: QueueRunnerAttrs): QueueRunner };
|
93 |
| - (jasmine as any).QueueRunner = class ZoneQueueRunner extends QueueRunner { |
94 |
| - constructor(attrs: QueueRunnerAttrs) { |
| 98 | + (jasmine as any).QueueRunner = (function (_super) { |
| 99 | + __extends(ZoneQueueRunner, _super); |
| 100 | + function ZoneQueueRunner(attrs) { |
95 | 101 | attrs.onComplete = ((fn) => () => {
|
96 | 102 | // All functions are done, clear the test zone.
|
97 | 103 | testProxyZone = null;
|
98 | 104 | ambientZone.scheduleMicroTask('jasmine.onComplete', fn);
|
99 | 105 | })(attrs.onComplete);
|
100 |
| - super(attrs); |
| 106 | + _super.call(this, attrs); |
101 | 107 | }
|
102 |
| - |
103 |
| - execute() { |
| 108 | + ZoneQueueRunner.prototype.execute = function () { |
104 | 109 | if(Zone.current !== ambientZone) throw new Error("Unexpected Zone: " + Zone.current.name);
|
105 | 110 | testProxyZone = ambientZone.fork(new ProxyZoneSpec());
|
106 | 111 | if (!Zone.currentTask) {
|
|
109 | 114 | // addEventListener callback would think that it is the top most task and would
|
110 | 115 | // drain the microtask queue on element.click() which would be incorrect.
|
111 | 116 | // For this reason we always force a task when running jasmine tests.
|
112 |
| - Zone.current.scheduleMicroTask('jasmine.execute().forceTask', () => super.execute()); |
| 117 | + Zone.current.scheduleMicroTask('jasmine.execute().forceTask', |
| 118 | + () => QueueRunner.prototype.execute.call(this)); |
113 | 119 | } else {
|
114 |
| - super.execute(); |
| 120 | + _super.prototype.execute.call(this); |
115 | 121 | }
|
116 |
| - } |
117 |
| - }; |
| 122 | + }; |
| 123 | + return ZoneQueueRunner; |
| 124 | + }(QueueRunner)); |
118 | 125 | })();
|
0 commit comments