jasmineAsyncInstall.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = jasmineAsyncInstall;
  6. var _co = _interopRequireDefault(require('co'));
  7. var _isGeneratorFn = _interopRequireDefault(require('is-generator-fn'));
  8. var _throat = _interopRequireDefault(require('throat'));
  9. var _isError = _interopRequireDefault(require('./isError'));
  10. function _interopRequireDefault(obj) {
  11. return obj && obj.__esModule ? obj : {default: obj};
  12. }
  13. var global = (function () {
  14. if (typeof globalThis !== 'undefined') {
  15. return globalThis;
  16. } else if (typeof global !== 'undefined') {
  17. return global;
  18. } else if (typeof self !== 'undefined') {
  19. return self;
  20. } else if (typeof window !== 'undefined') {
  21. return window;
  22. } else {
  23. return Function('return this')();
  24. }
  25. })();
  26. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  27. var global = (function () {
  28. if (typeof globalThis !== 'undefined') {
  29. return globalThis;
  30. } else if (typeof global !== 'undefined') {
  31. return global;
  32. } else if (typeof self !== 'undefined') {
  33. return self;
  34. } else if (typeof window !== 'undefined') {
  35. return window;
  36. } else {
  37. return Function('return this')();
  38. }
  39. })();
  40. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  41. var global = (function () {
  42. if (typeof globalThis !== 'undefined') {
  43. return globalThis;
  44. } else if (typeof global !== 'undefined') {
  45. return global;
  46. } else if (typeof self !== 'undefined') {
  47. return self;
  48. } else if (typeof window !== 'undefined') {
  49. return window;
  50. } else {
  51. return Function('return this')();
  52. }
  53. })();
  54. var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  55. function isPromise(obj) {
  56. return obj && typeof obj.then === 'function';
  57. }
  58. const doneFnNoop = () => {};
  59. doneFnNoop.fail = () => {};
  60. function promisifyLifeCycleFunction(originalFn, env) {
  61. return function (fn, timeout) {
  62. if (!fn) {
  63. // @ts-expect-error: missing fn arg is handled by originalFn
  64. return originalFn.call(env);
  65. }
  66. if (typeof fn !== 'function') {
  67. // Pass non-functions to Jest, which throws a nice error.
  68. return originalFn.call(env, fn, timeout);
  69. }
  70. const hasDoneCallback = fn.length > 0;
  71. if (hasDoneCallback) {
  72. // Give the function a name so it can be detected in call stacks, but
  73. // otherwise Jasmine will handle it.
  74. const asyncJestLifecycleWithCallback = function (...args) {
  75. // @ts-expect-error: Support possible extra args at runtime
  76. return fn.apply(this, args);
  77. };
  78. return originalFn.call(env, asyncJestLifecycleWithCallback, timeout);
  79. }
  80. const extraError = new Error(); // Without this line v8 stores references to all closures
  81. // in the stack in the Error object. This line stringifies the stack
  82. // property to allow garbage-collecting objects on the stack
  83. // https://crbug.com/v8/7142
  84. extraError.stack = extraError.stack; // We make *all* functions async and run `done` right away if they
  85. // didn't return a promise.
  86. const asyncJestLifecycle = function (done) {
  87. const wrappedFn = (0, _isGeneratorFn.default)(fn)
  88. ? _co.default.wrap(fn)
  89. : fn;
  90. const returnValue = wrappedFn.call({}, doneFnNoop);
  91. if (isPromise(returnValue)) {
  92. returnValue.then(done.bind(null, null), error => {
  93. const {isError: checkIsError, message} = (0, _isError.default)(error);
  94. if (message) {
  95. extraError.message = message;
  96. }
  97. done.fail(checkIsError ? error : extraError);
  98. });
  99. } else {
  100. done();
  101. }
  102. };
  103. return originalFn.call(env, asyncJestLifecycle, timeout);
  104. };
  105. } // Similar to promisifyLifeCycleFunction but throws an error
  106. // when the return value is neither a Promise nor `undefined`
  107. function promisifyIt(originalFn, env, jasmine) {
  108. return function (specName, fn, timeout) {
  109. if (!fn) {
  110. // @ts-expect-error: missing fn arg is handled by originalFn
  111. const spec = originalFn.call(env, specName);
  112. spec.pend('not implemented');
  113. return spec;
  114. }
  115. if (typeof fn !== 'function') {
  116. // Pass non-functions to Jest, which throws a nice error.
  117. return originalFn.call(env, specName, fn, timeout);
  118. }
  119. const hasDoneCallback = fn.length > 0;
  120. if (hasDoneCallback) {
  121. // Give the function a name so it can be detected in call stacks, but
  122. // otherwise Jasmine will handle it.
  123. const asyncJestTestWithCallback = function (...args) {
  124. // @ts-expect-error: Support possible extra args at runtime
  125. return fn.apply(this, args);
  126. };
  127. return originalFn.call(env, specName, asyncJestTestWithCallback, timeout);
  128. }
  129. const extraError = new Error(); // Without this line v8 stores references to all closures
  130. // in the stack in the Error object. This line stringifies the stack
  131. // property to allow garbage-collecting objects on the stack
  132. // https://crbug.com/v8/7142
  133. extraError.stack = extraError.stack;
  134. const asyncJestTest = function (done) {
  135. const wrappedFn = (0, _isGeneratorFn.default)(fn)
  136. ? _co.default.wrap(fn)
  137. : fn;
  138. const returnValue = wrappedFn.call({}, doneFnNoop);
  139. if (isPromise(returnValue)) {
  140. returnValue.then(done.bind(null, null), error => {
  141. const {isError: checkIsError, message} = (0, _isError.default)(error);
  142. if (message) {
  143. extraError.message = message;
  144. }
  145. if (jasmine.Spec.isPendingSpecException(error)) {
  146. env.pending(message);
  147. done();
  148. } else {
  149. done.fail(checkIsError ? error : extraError);
  150. }
  151. });
  152. } else if (returnValue === undefined) {
  153. done();
  154. } else {
  155. done.fail(
  156. new Error(
  157. 'Jest: `it` and `test` must return either a Promise or undefined.'
  158. )
  159. );
  160. }
  161. };
  162. return originalFn.call(env, specName, asyncJestTest, timeout);
  163. };
  164. }
  165. function makeConcurrent(originalFn, env, mutex) {
  166. const concurrentFn = function (specName, fn, timeout) {
  167. let promise = Promise.resolve();
  168. const spec = originalFn.call(env, specName, () => promise, timeout);
  169. if (env != null && !env.specFilter(spec)) {
  170. return spec;
  171. }
  172. try {
  173. promise = mutex(() => {
  174. const promise = fn();
  175. if (isPromise(promise)) {
  176. return promise;
  177. }
  178. throw new Error(
  179. `Jest: concurrent test "${spec.getFullName()}" must return a Promise.`
  180. );
  181. });
  182. } catch (error) {
  183. promise = Promise.reject(error);
  184. } // Avoid triggering the uncaught promise rejection handler in case the test errors before
  185. // being awaited on.
  186. promise.catch(() => {});
  187. return spec;
  188. }; // each is binded after the function is made concurrent, so for now it is made noop
  189. concurrentFn.each = () => () => {};
  190. return concurrentFn;
  191. }
  192. function jasmineAsyncInstall(globalConfig, global) {
  193. const jasmine = global.jasmine;
  194. const mutex = (0, _throat.default)(globalConfig.maxConcurrency);
  195. const env = jasmine.getEnv();
  196. env.it = promisifyIt(env.it, env, jasmine);
  197. env.fit = promisifyIt(env.fit, env, jasmine);
  198. global.it.concurrent = (env => {
  199. const concurrent = makeConcurrent(env.it, env, mutex);
  200. concurrent.only = makeConcurrent(env.fit, env, mutex);
  201. concurrent.skip = makeConcurrent(env.xit, env, mutex);
  202. return concurrent;
  203. })(env);
  204. global.fit.concurrent = makeConcurrent(env.fit, env, mutex);
  205. env.afterAll = promisifyLifeCycleFunction(env.afterAll, env);
  206. env.afterEach = promisifyLifeCycleFunction(env.afterEach, env);
  207. env.beforeAll = promisifyLifeCycleFunction(env.beforeAll, env);
  208. env.beforeEach = promisifyLifeCycleFunction(env.beforeEach, env);
  209. }