|
1 | 1 | 'use strict';
|
2 |
| -// Patch jasmine's it and fit functions so that the `done` wrapCallback always resets the zone |
3 |
| -// to the jasmine zone, which should be the root zone. (angular/zone.js#91) |
4 |
| -if (!Zone) { |
5 |
| - throw new Error('zone.js does not seem to be installed'); |
6 |
| -} |
7 |
| -// When you have in async test (test with `done` argument) jasmine will |
8 |
| -// execute the next test synchronously in the done handler. This makes sense |
9 |
| -// for most tests, but now with zones. With zones running next test |
10 |
| -// synchronously means that the current zone does not get cleared. This |
11 |
| -// results in a chain of nested zones, which makes it hard to reason about |
12 |
| -// it. We override the `clearStack` method which forces jasmine to always |
13 |
| -// drain the stack before next test gets executed. |
14 |
| -(<any>jasmine).QueueRunner = (function (SuperQueueRunner) { |
15 |
| - const originalZone = Zone.current; |
16 |
| - // Subclass the `QueueRunner` and override the `clearStack` method. |
17 |
| - |
18 |
| - function alwaysClearStack(fn) { |
19 |
| - const zone: Zone = Zone.current.getZoneWith('JasmineClearStackZone') |
20 |
| - || Zone.current.getZoneWith('ProxyZoneSpec') |
21 |
| - || originalZone; |
22 |
| - zone.scheduleMicroTask('jasmineCleanStack', fn); |
| 2 | +(() => { |
| 3 | + // Patch jasmine's describe/it/beforeEach/afterEach functions so test code always runs |
| 4 | + // in a testZone (ProxyZone). (See: angular/zone.js#91 & angular/angular#10503) |
| 5 | + if (!Zone) throw new Error("Missing: zone.js"); |
| 6 | + if (!jasmine) throw new Error("Missing: jasmine.js"); |
| 7 | + if (jasmine['__zone_patch__']) throw new Error("'jasmine' has already been patched with 'Zone'."); |
| 8 | + jasmine['__zone_patch__'] = true; |
| 9 | + |
| 10 | + const SyncTestZoneSpec: {new (name: string): ZoneSpec} = Zone['SyncTestZoneSpec']; |
| 11 | + const ProxyZoneSpec: {new (): ZoneSpec} = Zone['ProxyZoneSpec']; |
| 12 | + if (!SyncTestZoneSpec) throw new Error("Missing: SyncTestZoneSpec"); |
| 13 | + if (!ProxyZoneSpec) throw new Error("Missing: ProxyZoneSpec"); |
| 14 | + |
| 15 | + 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 |
| 18 | + // a `beforeEach` or `it`. |
| 19 | + const syncZone = ambientZone.fork(new SyncTestZoneSpec('jasmine.describe')); |
| 20 | + |
| 21 | + // This is the zone which will be used for running individual tests. |
| 22 | + // It will be a proxy zone, so that the tests function can retroactively install |
| 23 | + // different zones. |
| 24 | + // Example: |
| 25 | + // - In beforeEach() do childZone = Zone.current.fork(...); |
| 26 | + // - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the |
| 27 | + // zone outside of fakeAsync it will be able to escope the fakeAsync rules. |
| 28 | + // - Because ProxyZone is parent fo `childZone` fakeAsync can retroactively add |
| 29 | + // fakeAsync behavior to the childZone. |
| 30 | + let testProxyZone: Zone = null; |
| 31 | + |
| 32 | + // Monkey patch all of the jasmine DSL so that each function runs in appropriate zone. |
| 33 | + const jasmineEnv = jasmine.getEnv(); |
| 34 | + ['desribe', 'xdescribe', 'fdescribe'].forEach((methodName) => { |
| 35 | + let originalJasmineFn: Function = jasmineEnv[methodName]; |
| 36 | + jasmineEnv[methodName] = function(description: string, specDefinitions: Function) { |
| 37 | + return originalJasmineFn.call(this, description, wrapDescribeInZone(specDefinitions)); |
| 38 | + } |
| 39 | + }); |
| 40 | + ['it', 'xit', 'fit'].forEach((methodName) => { |
| 41 | + let originalJasmineFn: Function = jasmineEnv[methodName]; |
| 42 | + jasmineEnv[methodName] = function(description: string, specDefinitions: Function) { |
| 43 | + return originalJasmineFn.call(this, description, wrapTestInZone(specDefinitions)); |
| 44 | + } |
| 45 | + }); |
| 46 | + ['beforeEach', 'afterEach'].forEach((methodName) => { |
| 47 | + let originalJasmineFn: Function = jasmineEnv[methodName]; |
| 48 | + jasmineEnv[methodName] = function(specDefinitions: Function) { |
| 49 | + return originalJasmineFn.call(this, wrapTestInZone(specDefinitions)); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets a function wrapping the body of a Jasmine `describe` block to execute in a |
| 55 | + * synchronous-only zone. |
| 56 | + */ |
| 57 | + function wrapDescribeInZone(describeBody: Function): Function { |
| 58 | + return function() { |
| 59 | + return syncZone.run(describeBody, this, arguments as any as any[]); |
| 60 | + } |
23 | 61 | }
|
24 | 62 |
|
25 |
| - function QueueRunner(options) { |
26 |
| - options.clearStack = alwaysClearStack; |
27 |
| - SuperQueueRunner.call(this, options); |
| 63 | + /** |
| 64 | + * Gets a function wrapping the body of a Jasmine `it/beforeEach/afterEach` block to |
| 65 | + * execute in a ProxyZone zone. |
| 66 | + * This will run in `testProxyZone`. The `testProxyZone` will be reset by the `ZoneQueueRunner` |
| 67 | + */ |
| 68 | + function wrapTestInZone(testBody: Function): Function { |
| 69 | + // The `done` callback is only passed through if the function expects at least one argument. |
| 70 | + // Note we have to make a function with correct number of arguments, otherwise jasmine will |
| 71 | + // think that all functions are sync or async. |
| 72 | + return (testBody.length == 0) |
| 73 | + ? function() { return testProxyZone.run(testBody, this); } |
| 74 | + : function(done) { return testProxyZone.run(testBody, this, [done]); }; |
| 75 | + } |
| 76 | + interface QueueRunner { |
| 77 | + execute(): void; |
28 | 78 | }
|
29 |
| - QueueRunner.prototype = SuperQueueRunner.prototype; |
30 |
| - return QueueRunner; |
31 |
| -})((<any>jasmine).QueueRunner); |
| 79 | + interface QueueRunnerAttrs { |
| 80 | + queueableFns: {fn: Function}[]; |
| 81 | + onComplete: () => void; |
| 82 | + clearStack: (fn) => void; |
| 83 | + onException: (error) => void; |
| 84 | + catchException: () => boolean; |
| 85 | + userContext: any; |
| 86 | + timeout: {setTimeout: Function, clearTimeout: Function}; |
| 87 | + fail: ()=> void; |
| 88 | + } |
| 89 | + |
| 90 | + const QueueRunner = (jasmine as any).QueueRunner as { new(attrs: QueueRunnerAttrs): QueueRunner }; |
| 91 | + (jasmine as any).QueueRunner = class ZoneQueueRunner extends QueueRunner { |
| 92 | + constructor(attrs: QueueRunnerAttrs) { |
| 93 | + attrs.clearStack = (fn) => fn(); // Don't clear since onComplete will clear. |
| 94 | + attrs.onComplete = ((fn) => () => { |
| 95 | + // All functions are done, clear the test zone. |
| 96 | + testProxyZone = null; |
| 97 | + ambientZone.scheduleMicroTask('jasmine.onComplete', fn); |
| 98 | + })(attrs.onComplete); |
| 99 | + super(attrs); |
| 100 | + } |
32 | 101 |
|
| 102 | + execute() { |
| 103 | + if(Zone.current !== ambientZone) throw new Error("Unexpected Zone: " + Zone.current.name); |
| 104 | + testProxyZone = ambientZone.fork(new ProxyZoneSpec()); |
| 105 | + super.execute(); |
| 106 | + } |
| 107 | + }; |
| 108 | +})(); |
0 commit comments