|
| 1 | +/** |
| 2 | + * Patch Jest's describe/test/beforeEach/afterEach functions so test code |
| 3 | + * always runs in a testZone (ProxyZone). |
| 4 | +*/ |
| 5 | + |
| 6 | +if (Zone === undefined) { |
| 7 | + throw new Error('Missing: Zone (zone.js)'); |
| 8 | +} |
| 9 | +if (jest === undefined) { |
| 10 | + throw new Error( |
| 11 | + 'Missing: jest.\n' + |
| 12 | + 'This patch must be included in a script called with ' + |
| 13 | + '`setupTestFrameworkScriptFile` in Jest config.' |
| 14 | + ); |
| 15 | +} |
| 16 | +if (jest['__zone_patch__'] === true) { |
| 17 | + throw new Error("'jest' has already been patched with 'Zone'."); |
| 18 | +} |
| 19 | + |
| 20 | +jest['__zone_patch__'] = true; |
| 21 | +const SyncTestZoneSpec = Zone['SyncTestZoneSpec']; |
| 22 | +const ProxyZoneSpec = Zone['ProxyZoneSpec']; |
| 23 | + |
| 24 | +if (SyncTestZoneSpec === undefined) { |
| 25 | + throw new Error('Missing: SyncTestZoneSpec (zone.js/dist/sync-test)'); |
| 26 | +} |
| 27 | +if (ProxyZoneSpec === undefined) { |
| 28 | + throw new Error('Missing: ProxyZoneSpec (zone.js/dist/proxy.js)'); |
| 29 | +} |
| 30 | + |
| 31 | +const env = global; |
| 32 | +const ambientZone = Zone.current; |
| 33 | + |
| 34 | +// Create a synchronous-only zone in which to run `describe` blocks in order to |
| 35 | +// raise an error if any asynchronous operations are attempted |
| 36 | +// inside of a `describe` but outside of a `beforeEach` or `it`. |
| 37 | +const syncZone = ambientZone.fork(new SyncTestZoneSpec('jest.describe')); |
| 38 | +function wrapDescribeInZone(describeBody) { |
| 39 | + return function () { return syncZone.run(describeBody, null, arguments); } |
| 40 | +} |
| 41 | + |
| 42 | +// Create a proxy zone in which to run `test` blocks so that the tests function |
| 43 | +// can retroactively install different zones. |
| 44 | +const testProxyZone = ambientZone.fork(new ProxyZoneSpec()); |
| 45 | +function wrapTestInZone(testBody) { |
| 46 | + if (testBody === undefined) { |
| 47 | + return; |
| 48 | + } |
| 49 | + return testBody.length === 0 |
| 50 | + ? () => testProxyZone.run(testBody, null) |
| 51 | + : done => testProxyZone.run(testBody, null, [done]); |
| 52 | +} |
| 53 | + |
| 54 | +const bindDescribe = (originalJestFn) => function () { |
| 55 | + const eachArguments = arguments; |
| 56 | + return function (description, specDefinitions, timeout) { |
| 57 | + arguments[1] = wrapDescribeInZone(specDefinitions) |
| 58 | + return originalJestFn.apply(this, eachArguments).apply( |
| 59 | + this, |
| 60 | + arguments |
| 61 | + ) |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +['xdescribe', 'fdescribe', 'describe'].forEach(methodName => { |
| 66 | + const originaljestFn = env[methodName]; |
| 67 | + env[methodName] = function(description, specDefinitions, timeout) { |
| 68 | + arguments[1] = wrapDescribeInZone(specDefinitions) |
| 69 | + return originaljestFn.apply( |
| 70 | + this, |
| 71 | + arguments |
| 72 | + ); |
| 73 | + }; |
| 74 | + env[methodName].each = bindDescribe(originaljestFn.each); |
| 75 | + if (methodName === 'describe') { |
| 76 | + env[methodName].only = env['fdescribe']; |
| 77 | + env[methodName].skip = env['xdescribe']; |
| 78 | + env[methodName].only.each = bindDescribe(originaljestFn.only.each); |
| 79 | + env[methodName].skip.each = bindDescribe(originaljestFn.skip.each); |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | +['xit', 'fit', 'xtest', 'test', 'it'].forEach(methodName => { |
| 84 | + const originaljestFn = env[methodName]; |
| 85 | + env[methodName] = function(description, specDefinitions, timeout) { |
| 86 | + arguments[1] = wrapTestInZone(specDefinitions); |
| 87 | + return originaljestFn.apply(this, arguments); |
| 88 | + }; |
| 89 | + // The revised method will be populated to the final each method, so we only declare the method that in the new globals |
| 90 | + env[methodName].each = originaljestFn.each; |
| 91 | + if (methodName === 'test' || methodName === 'it') { |
| 92 | + env[methodName].only = env['fit']; |
| 93 | + env[methodName].only.each = originaljestFn.only.each; |
| 94 | + |
| 95 | + env[methodName].skip = env['xit']; |
| 96 | + env[methodName].skip.each = originaljestFn.skip.each; |
| 97 | + } |
| 98 | +}); |
| 99 | + |
| 100 | +['beforeEach', 'afterEach', 'beforeAll', 'afterAll'].forEach(methodName => { |
| 101 | + const originaljestFn = env[methodName]; |
| 102 | + env[methodName] = function(specDefinitions, timeout) { |
| 103 | + arguments[0] = wrapTestInZone(specDefinitions); |
| 104 | + return originaljestFn.apply(this, arguments); |
| 105 | + }; |
| 106 | +}); |
0 commit comments