|
| 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 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +Zone.__load_patch('jest', (context: any, Zone: ZoneType) => { |
| 12 | + if (typeof jest === 'undefined' || jest['__zone_patch__']) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + jest['__zone_patch__'] = true; |
| 17 | + |
| 18 | + |
| 19 | + if (typeof Zone === 'undefined') { |
| 20 | + throw new Error('Missing Zone.js'); |
| 21 | + } |
| 22 | + |
| 23 | + const ProxyZoneSpec = (Zone as any)['ProxyZoneSpec']; |
| 24 | + const SyncTestZoneSpec = (Zone as any)['SyncTestZoneSpec']; |
| 25 | + |
| 26 | + if (!ProxyZoneSpec) { |
| 27 | + throw new Error('Missing ProxyZoneSpec'); |
| 28 | + } |
| 29 | + |
| 30 | + const rootZone = Zone.current; |
| 31 | + const syncZone = rootZone.fork(new SyncTestZoneSpec('jest.describe')); |
| 32 | + const proxyZone = rootZone.fork(new ProxyZoneSpec()); |
| 33 | + |
| 34 | + function wrapDescribeFactoryInZone(originalJestFn: Function) { |
| 35 | + return function(this: unknown, ...tableArgs: any[]) { |
| 36 | + const originalDescribeFn = originalJestFn.apply(this, tableArgs); |
| 37 | + return function(this: unknown, ...args: any[]) { |
| 38 | + args[1] = wrapDescribeInZone(args[1]); |
| 39 | + return originalDescribeFn.apply(this, args); |
| 40 | + }; |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + function wrapTestFactoryInZone(originalJestFn: Function) { |
| 45 | + return function(this: unknown, ...tableArgs: any[]) { |
| 46 | + const testFn = originalJestFn.apply(this, tableArgs); |
| 47 | + return function(this: unknown, ...args: any[]) { |
| 48 | + args[1] = wrapTestInZone(args[1]); |
| 49 | + return testFn.apply(this, args); |
| 50 | + }; |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets a function wrapping the body of a jest `describe` block to execute in a |
| 56 | + * synchronous-only zone. |
| 57 | + */ |
| 58 | + function wrapDescribeInZone(describeBody: Function): Function { |
| 59 | + return function(this: unknown, ...args: any[]) { |
| 60 | + return syncZone.run(describeBody, this, args); |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Gets a function wrapping the body of a jest `it/beforeEach/afterEach` block to |
| 66 | + * execute in a ProxyZone zone. |
| 67 | + * This will run in the `testProxyZone`. |
| 68 | + */ |
| 69 | + function wrapTestInZone(testBody: Function): Function { |
| 70 | + if (typeof testBody !== 'function') { |
| 71 | + return testBody; |
| 72 | + } |
| 73 | + // The `done` callback is only passed through if the function expects at least one argument. |
| 74 | + // Note we have to make a function with correct number of arguments, otherwise jest will |
| 75 | + // think that all functions are sync or async. |
| 76 | + return function(this: unknown, ...args: any[]) { return proxyZone.run(testBody, this, args); }; |
| 77 | + } |
| 78 | + |
| 79 | + ['describe', 'xdescribe', 'fdescribe'].forEach(methodName => { |
| 80 | + let originalJestFn: Function = context[methodName]; |
| 81 | + if (context[Zone.__symbol__(methodName)]) { |
| 82 | + return; |
| 83 | + } |
| 84 | + context[Zone.__symbol__(methodName)] = originalJestFn; |
| 85 | + context[methodName] = function(this: unknown, ...args: any[]) { |
| 86 | + args[1] = wrapDescribeInZone(args[1]); |
| 87 | + return originalJestFn.apply(this, args); |
| 88 | + }; |
| 89 | + context[methodName].each = wrapDescribeFactoryInZone((originalJestFn as any).each); |
| 90 | + }); |
| 91 | + context.describe.only = context.fdescribe; |
| 92 | + context.describe.skip = context.xdescribe; |
| 93 | + |
| 94 | + ['it', 'xit', 'fit', 'test', 'xtest'].forEach(methodName => { |
| 95 | + let originalJestFn: Function = context[methodName]; |
| 96 | + if (context[Zone.__symbol__(methodName)]) { |
| 97 | + return; |
| 98 | + } |
| 99 | + context[Zone.__symbol__(methodName)] = originalJestFn; |
| 100 | + context[methodName] = function(this: unknown, ...args: any[]) { |
| 101 | + args[1] = wrapTestInZone(args[1]); |
| 102 | + return originalJestFn.apply(this, args); |
| 103 | + }; |
| 104 | + context[methodName].each = wrapTestFactoryInZone((originalJestFn as any).each); |
| 105 | + context[methodName].todo = (originalJestFn as any).todo; |
| 106 | + }); |
| 107 | + |
| 108 | + context.it.only = context.fit; |
| 109 | + context.it.skip = context.xit; |
| 110 | + context.test.only = context.fit; |
| 111 | + context.test.skip = context.xit; |
| 112 | + |
| 113 | + ['beforeEach', 'afterEach', 'beforeAll', 'afterAll'].forEach(methodName => { |
| 114 | + let originalJestFn: Function = context[methodName]; |
| 115 | + if (context[Zone.__symbol__(methodName)]) { |
| 116 | + return; |
| 117 | + } |
| 118 | + context[Zone.__symbol__(methodName)] = originalJestFn; |
| 119 | + context[methodName] = function(this: unknown, ...args: any[]) { |
| 120 | + args[0] = wrapTestInZone(args[0]); |
| 121 | + return originalJestFn.apply(this, args); |
| 122 | + }; |
| 123 | + }); |
| 124 | +}); |
0 commit comments