|
| 1 | +'use strict' |
| 2 | + |
| 3 | +require('core-js/es6/reflect') |
| 4 | +require('core-js/es7/reflect') |
| 5 | +require('zone.js/dist/zone.js') |
| 6 | +require('zone.js/dist/proxy.js') |
| 7 | +require('zone.js/dist/sync-test') |
| 8 | +require('zone.js/dist/async-test') |
| 9 | +require('zone.js/dist/fake-async-test') |
| 10 | + |
| 11 | +/** |
| 12 | + * Patch Jest's describe/test/beforeEach/afterEach functions so test code |
| 13 | + * always runs in a testZone (ProxyZone). |
| 14 | + */ |
| 15 | + |
| 16 | +if (Zone === undefined) { |
| 17 | + throw new Error('Missing: Zone (zone.js)') |
| 18 | +} |
| 19 | +if (jest === undefined) { |
| 20 | + throw new Error( |
| 21 | + 'Missing: jest.\n' + |
| 22 | + 'This patch must be included in a script called with ' + |
| 23 | + '`setupTestFrameworkScriptFile` in Jest config.' |
| 24 | + ) |
| 25 | +} |
| 26 | +if (jest['__zone_patch__'] === true) { |
| 27 | + throw new Error("'jest' has already been patched with 'Zone'.") |
| 28 | +} |
| 29 | + |
| 30 | +jest['__zone_patch__'] = true |
| 31 | +const SyncTestZoneSpec = Zone['SyncTestZoneSpec'] |
| 32 | +const ProxyZoneSpec = Zone['ProxyZoneSpec'] |
| 33 | + |
| 34 | +if (SyncTestZoneSpec === undefined) { |
| 35 | + throw new Error('Missing: SyncTestZoneSpec (zone.js/dist/sync-test)') |
| 36 | +} |
| 37 | +if (ProxyZoneSpec === undefined) { |
| 38 | + throw new Error('Missing: ProxyZoneSpec (zone.js/dist/proxy.js)') |
| 39 | +} |
| 40 | + |
| 41 | +const env = global |
| 42 | +const ambientZone = Zone.current |
| 43 | + |
| 44 | +// Create a synchronous-only zone in which to run `describe` blocks in order to |
| 45 | +// raise an error if any asynchronous operations are attempted |
| 46 | +// inside of a `describe` but outside of a `beforeEach` or `it`. |
| 47 | +const syncZone = ambientZone.fork(new SyncTestZoneSpec('jest.describe')) |
| 48 | +function wrapDescribeInZone(describeBody) { |
| 49 | + return () => syncZone.run(describeBody, null, arguments) |
| 50 | +} |
| 51 | + |
| 52 | +// Create a proxy zone in which to run `test` blocks so that the tests function |
| 53 | +// can retroactively install different zones. |
| 54 | +const testProxyZone = ambientZone.fork(new ProxyZoneSpec()) |
| 55 | +function wrapTestInZone(testBody) { |
| 56 | + if (testBody === undefined) { |
| 57 | + return |
| 58 | + } |
| 59 | + return testBody.length === 0 |
| 60 | + ? () => testProxyZone.run(testBody, null) |
| 61 | + : done => testProxyZone.run(testBody, null, [done]) |
| 62 | +} |
| 63 | + |
| 64 | +;['xdescribe', 'fdescribe', 'describe'].forEach(methodName => { |
| 65 | + const originaljestFn = env[methodName] |
| 66 | + env[methodName] = function(description, specDefinitions) { |
| 67 | + return originaljestFn.call( |
| 68 | + this, |
| 69 | + description, |
| 70 | + wrapDescribeInZone(specDefinitions) |
| 71 | + ) |
| 72 | + } |
| 73 | + if (methodName === 'describe') { |
| 74 | + env[methodName].only = env['fdescribe'] |
| 75 | + env[methodName].skip = env['xdescribe'] |
| 76 | + } |
| 77 | +}) |
| 78 | + |
| 79 | +;['xit', 'fit', 'test', 'it'].forEach(methodName => { |
| 80 | + const originaljestFn = env[methodName] |
| 81 | + env[methodName] = function(description, specDefinitions, timeout) { |
| 82 | + arguments[1] = wrapTestInZone(specDefinitions) |
| 83 | + return originaljestFn.apply(this, arguments) |
| 84 | + } |
| 85 | + if (methodName === 'test' || methodName === 'it') { |
| 86 | + env[methodName].only = env['fit'] |
| 87 | + env[methodName].skip = env['xit'] |
| 88 | + } |
| 89 | +}) |
| 90 | + |
| 91 | +;['beforeEach', 'afterEach', 'beforeAll', 'afterAll'].forEach(methodName => { |
| 92 | + const originaljestFn = env[methodName] |
| 93 | + env[methodName] = function(specDefinitions, timeout) { |
| 94 | + arguments[0] = wrapTestInZone(specDefinitions) |
| 95 | + return originaljestFn.apply(this, arguments) |
| 96 | + } |
| 97 | +}) |
| 98 | + |
| 99 | +// const AngularSnapshotSerializer = require('./AngularSnapshotSerializer'); |
| 100 | +// const HTMLCommentSerializer = require('./HTMLCommentSerializer'); |
| 101 | +const getTestBed = require('@angular/core/testing').getTestBed |
| 102 | +const BrowserDynamicTestingModule = require('@angular/platform-browser-dynamic/testing') |
| 103 | + .BrowserDynamicTestingModule |
| 104 | +const platformBrowserDynamicTesting = require('@angular/platform-browser-dynamic/testing') |
| 105 | + .platformBrowserDynamicTesting |
| 106 | + |
| 107 | +getTestBed().initTestEnvironment( |
| 108 | + BrowserDynamicTestingModule, |
| 109 | + platformBrowserDynamicTesting() |
| 110 | +) |
| 111 | + |
| 112 | +const mock = () => { |
| 113 | + let storage = {} |
| 114 | + return { |
| 115 | + getItem: key => (key in storage ? storage[key] : null), |
| 116 | + setItem: (key, value) => (storage[key] = value || ''), |
| 117 | + removeItem: key => delete storage[key], |
| 118 | + clear: () => (storage = {}) |
| 119 | + } |
| 120 | +} |
| 121 | +// Object.defineProperty(window, 'Hammer', { value: {} }); |
| 122 | +Object.defineProperty(window, 'CSS', { value: mock() }) |
| 123 | +Object.defineProperty(window, 'matchMedia', { |
| 124 | + value: jest.fn(() => ({ matches: true })) |
| 125 | +}) |
| 126 | +Object.defineProperty(window, 'localStorage', { value: mock() }) |
| 127 | +Object.defineProperty(window, 'sessionStorage', { value: mock() }) |
| 128 | +Object.defineProperty(window, 'getComputedStyle', { |
| 129 | + value: () => { |
| 130 | + return { |
| 131 | + display: 'none', |
| 132 | + appearance: ['-webkit-appearance'] |
| 133 | + } |
| 134 | + } |
| 135 | +}) |
| 136 | + |
| 137 | +// For Angular Material |
| 138 | +Object.defineProperty(document.body.style, 'transform', { |
| 139 | + value: () => { |
| 140 | + return { |
| 141 | + enumerable: true, |
| 142 | + configurable: true |
| 143 | + } |
| 144 | + } |
| 145 | +}) |
0 commit comments