1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = jasmineAsyncInstall;
|
7 |
|
8 | var _co = _interopRequireDefault(require('co'));
|
9 |
|
10 | var _isGeneratorFn = _interopRequireDefault(require('is-generator-fn'));
|
11 |
|
12 | var _throat = _interopRequireDefault(require('throat'));
|
13 |
|
14 | var _isError = _interopRequireDefault(require('./isError'));
|
15 |
|
16 | function _interopRequireDefault(obj) {
|
17 | return obj && obj.__esModule ? obj : {default: obj};
|
18 | }
|
19 |
|
20 | var global = (function () {
|
21 | if (typeof globalThis !== 'undefined') {
|
22 | return globalThis;
|
23 | } else if (typeof global !== 'undefined') {
|
24 | return global;
|
25 | } else if (typeof self !== 'undefined') {
|
26 | return self;
|
27 | } else if (typeof window !== 'undefined') {
|
28 | return window;
|
29 | } else {
|
30 | return Function('return this')();
|
31 | }
|
32 | })();
|
33 |
|
34 | var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
35 |
|
36 | var global = (function () {
|
37 | if (typeof globalThis !== 'undefined') {
|
38 | return globalThis;
|
39 | } else if (typeof global !== 'undefined') {
|
40 | return global;
|
41 | } else if (typeof self !== 'undefined') {
|
42 | return self;
|
43 | } else if (typeof window !== 'undefined') {
|
44 | return window;
|
45 | } else {
|
46 | return Function('return this')();
|
47 | }
|
48 | })();
|
49 |
|
50 | var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
51 |
|
52 | var global = (function () {
|
53 | if (typeof globalThis !== 'undefined') {
|
54 | return globalThis;
|
55 | } else if (typeof global !== 'undefined') {
|
56 | return global;
|
57 | } else if (typeof self !== 'undefined') {
|
58 | return self;
|
59 | } else if (typeof window !== 'undefined') {
|
60 | return window;
|
61 | } else {
|
62 | return Function('return this')();
|
63 | }
|
64 | })();
|
65 |
|
66 | var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
|
67 |
|
68 | function isPromise(obj) {
|
69 | return obj && typeof obj.then === 'function';
|
70 | }
|
71 |
|
72 | const doneFnNoop = () => {};
|
73 |
|
74 | doneFnNoop.fail = () => {};
|
75 |
|
76 | function promisifyLifeCycleFunction(originalFn, env) {
|
77 | return function (fn, timeout) {
|
78 | if (!fn) {
|
79 |
|
80 | return originalFn.call(env);
|
81 | }
|
82 |
|
83 | if (typeof fn !== 'function') {
|
84 |
|
85 | return originalFn.call(env, fn, timeout);
|
86 | }
|
87 |
|
88 | const hasDoneCallback = fn.length > 0;
|
89 |
|
90 | if (hasDoneCallback) {
|
91 |
|
92 |
|
93 | const asyncJestLifecycleWithCallback = function (...args) {
|
94 |
|
95 | return fn.apply(this, args);
|
96 | };
|
97 |
|
98 | return originalFn.call(env, asyncJestLifecycleWithCallback, timeout);
|
99 | }
|
100 |
|
101 | const extraError = new Error();
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | extraError.stack = extraError.stack;
|
107 |
|
108 |
|
109 | const asyncJestLifecycle = function (done) {
|
110 | const wrappedFn = (0, _isGeneratorFn.default)(fn)
|
111 | ? _co.default.wrap(fn)
|
112 | : fn;
|
113 | const returnValue = wrappedFn.call({}, doneFnNoop);
|
114 |
|
115 | if (isPromise(returnValue)) {
|
116 | returnValue.then(done.bind(null, null), error => {
|
117 | const {isError: checkIsError, message} = (0, _isError.default)(error);
|
118 |
|
119 | if (message) {
|
120 | extraError.message = message;
|
121 | }
|
122 |
|
123 | done.fail(checkIsError ? error : extraError);
|
124 | });
|
125 | } else {
|
126 | done();
|
127 | }
|
128 | };
|
129 |
|
130 | return originalFn.call(env, asyncJestLifecycle, timeout);
|
131 | };
|
132 | }
|
133 |
|
134 |
|
135 | function promisifyIt(originalFn, env, jasmine) {
|
136 | return function (specName, fn, timeout) {
|
137 | if (!fn) {
|
138 |
|
139 | const spec = originalFn.call(env, specName);
|
140 | spec.pend('not implemented');
|
141 | return spec;
|
142 | }
|
143 |
|
144 | if (typeof fn !== 'function') {
|
145 |
|
146 | return originalFn.call(env, specName, fn, timeout);
|
147 | }
|
148 |
|
149 | const hasDoneCallback = fn.length > 0;
|
150 |
|
151 | if (hasDoneCallback) {
|
152 |
|
153 |
|
154 | const asyncJestTestWithCallback = function (...args) {
|
155 |
|
156 | return fn.apply(this, args);
|
157 | };
|
158 |
|
159 | return originalFn.call(env, specName, asyncJestTestWithCallback, timeout);
|
160 | }
|
161 |
|
162 | const extraError = new Error();
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | extraError.stack = extraError.stack;
|
168 |
|
169 | const asyncJestTest = function (done) {
|
170 | const wrappedFn = (0, _isGeneratorFn.default)(fn)
|
171 | ? _co.default.wrap(fn)
|
172 | : fn;
|
173 | const returnValue = wrappedFn.call({}, doneFnNoop);
|
174 |
|
175 | if (isPromise(returnValue)) {
|
176 | returnValue.then(done.bind(null, null), error => {
|
177 | const {isError: checkIsError, message} = (0, _isError.default)(error);
|
178 |
|
179 | if (message) {
|
180 | extraError.message = message;
|
181 | }
|
182 |
|
183 | if (jasmine.Spec.isPendingSpecException(error)) {
|
184 | env.pending(message);
|
185 | done();
|
186 | } else {
|
187 | done.fail(checkIsError ? error : extraError);
|
188 | }
|
189 | });
|
190 | } else if (returnValue === undefined) {
|
191 | done();
|
192 | } else {
|
193 | done.fail(
|
194 | new Error(
|
195 | 'Jest: `it` and `test` must return either a Promise or undefined.'
|
196 | )
|
197 | );
|
198 | }
|
199 | };
|
200 |
|
201 | return originalFn.call(env, specName, asyncJestTest, timeout);
|
202 | };
|
203 | }
|
204 |
|
205 | function makeConcurrent(originalFn, env, mutex) {
|
206 | const concurrentFn = function (specName, fn, timeout) {
|
207 | let promise = Promise.resolve();
|
208 | const spec = originalFn.call(env, specName, () => promise, timeout);
|
209 |
|
210 | if (env != null && !env.specFilter(spec)) {
|
211 | return spec;
|
212 | }
|
213 |
|
214 | try {
|
215 | promise = mutex(() => {
|
216 | const promise = fn();
|
217 |
|
218 | if (isPromise(promise)) {
|
219 | return promise;
|
220 | }
|
221 |
|
222 | throw new Error(
|
223 | `Jest: concurrent test "${spec.getFullName()}" must return a Promise.`
|
224 | );
|
225 | });
|
226 | } catch (error) {
|
227 | promise = Promise.reject(error);
|
228 | }
|
229 |
|
230 | return spec;
|
231 | };
|
232 |
|
233 | concurrentFn.each = () => () => {};
|
234 |
|
235 | return concurrentFn;
|
236 | }
|
237 |
|
238 | function jasmineAsyncInstall(globalConfig, global) {
|
239 | const jasmine = global.jasmine;
|
240 | const mutex = (0, _throat.default)(globalConfig.maxConcurrency);
|
241 | const env = jasmine.getEnv();
|
242 | env.it = promisifyIt(env.it, env, jasmine);
|
243 | env.fit = promisifyIt(env.fit, env, jasmine);
|
244 |
|
245 | global.it.concurrent = (env => {
|
246 | const concurrent = makeConcurrent(env.it, env, mutex);
|
247 | concurrent.only = makeConcurrent(env.fit, env, mutex);
|
248 | concurrent.skip = makeConcurrent(env.xit, env, mutex);
|
249 | return concurrent;
|
250 | })(env);
|
251 |
|
252 | global.fit.concurrent = makeConcurrent(env.fit, env, mutex);
|
253 | env.afterAll = promisifyLifeCycleFunction(env.afterAll, env);
|
254 | env.afterEach = promisifyLifeCycleFunction(env.afterEach, env);
|
255 | env.beforeAll = promisifyLifeCycleFunction(env.beforeAll, env);
|
256 | env.beforeEach = promisifyLifeCycleFunction(env.beforeEach, env);
|
257 | }
|