testWorker.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.setup = setup;
  6. exports.worker = worker;
  7. function _jestHasteMap() {
  8. const data = _interopRequireDefault(require('jest-haste-map'));
  9. _jestHasteMap = function _jestHasteMap() {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _exit() {
  15. const data = _interopRequireDefault(require('exit'));
  16. _exit = function _exit() {
  17. return data;
  18. };
  19. return data;
  20. }
  21. function _jestMessageUtil() {
  22. const data = require('jest-message-util');
  23. _jestMessageUtil = function _jestMessageUtil() {
  24. return data;
  25. };
  26. return data;
  27. }
  28. function _jestRuntime() {
  29. const data = _interopRequireDefault(require('jest-runtime'));
  30. _jestRuntime = function _jestRuntime() {
  31. return data;
  32. };
  33. return data;
  34. }
  35. var _runTest = _interopRequireDefault(require('./runTest'));
  36. function _interopRequireDefault(obj) {
  37. return obj && obj.__esModule ? obj : {default: obj};
  38. }
  39. function _objectSpread(target) {
  40. for (var i = 1; i < arguments.length; i++) {
  41. var source = arguments[i] != null ? arguments[i] : {};
  42. var ownKeys = Object.keys(source);
  43. if (typeof Object.getOwnPropertySymbols === 'function') {
  44. ownKeys = ownKeys.concat(
  45. Object.getOwnPropertySymbols(source).filter(function(sym) {
  46. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  47. })
  48. );
  49. }
  50. ownKeys.forEach(function(key) {
  51. _defineProperty(target, key, source[key]);
  52. });
  53. }
  54. return target;
  55. }
  56. function _defineProperty(obj, key, value) {
  57. if (key in obj) {
  58. Object.defineProperty(obj, key, {
  59. value: value,
  60. enumerable: true,
  61. configurable: true,
  62. writable: true
  63. });
  64. } else {
  65. obj[key] = value;
  66. }
  67. return obj;
  68. }
  69. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  70. try {
  71. var info = gen[key](arg);
  72. var value = info.value;
  73. } catch (error) {
  74. reject(error);
  75. return;
  76. }
  77. if (info.done) {
  78. resolve(value);
  79. } else {
  80. Promise.resolve(value).then(_next, _throw);
  81. }
  82. }
  83. function _asyncToGenerator(fn) {
  84. return function() {
  85. var self = this,
  86. args = arguments;
  87. return new Promise(function(resolve, reject) {
  88. var gen = fn.apply(self, args);
  89. function _next(value) {
  90. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'next', value);
  91. }
  92. function _throw(err) {
  93. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'throw', err);
  94. }
  95. _next(undefined);
  96. });
  97. };
  98. }
  99. // Make sure uncaught errors are logged before we exit.
  100. process.on('uncaughtException', err => {
  101. console.error(err.stack);
  102. (0, _exit().default)(1);
  103. });
  104. const formatError = error => {
  105. if (typeof error === 'string') {
  106. const _separateMessageFromS = (0,
  107. _jestMessageUtil().separateMessageFromStack)(error),
  108. message = _separateMessageFromS.message,
  109. stack = _separateMessageFromS.stack;
  110. return {
  111. message,
  112. stack,
  113. type: 'Error'
  114. };
  115. }
  116. return {
  117. code: error.code || undefined,
  118. message: error.message,
  119. stack: error.stack,
  120. type: 'Error'
  121. };
  122. };
  123. const resolvers = new Map();
  124. const getResolver = config => {
  125. const resolver = resolvers.get(config.name);
  126. if (!resolver) {
  127. throw new Error('Cannot find resolver for: ' + config.name);
  128. }
  129. return resolver;
  130. };
  131. function setup(setupData) {
  132. // Module maps that will be needed for the test runs are passed.
  133. var _iteratorNormalCompletion = true;
  134. var _didIteratorError = false;
  135. var _iteratorError = undefined;
  136. try {
  137. for (
  138. var _iterator = setupData.serializableResolvers[Symbol.iterator](), _step;
  139. !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
  140. _iteratorNormalCompletion = true
  141. ) {
  142. const _step$value = _step.value,
  143. config = _step$value.config,
  144. serializableModuleMap = _step$value.serializableModuleMap;
  145. const moduleMap = _jestHasteMap().default.ModuleMap.fromJSON(
  146. serializableModuleMap
  147. );
  148. resolvers.set(
  149. config.name,
  150. _jestRuntime().default.createResolver(config, moduleMap)
  151. );
  152. }
  153. } catch (err) {
  154. _didIteratorError = true;
  155. _iteratorError = err;
  156. } finally {
  157. try {
  158. if (!_iteratorNormalCompletion && _iterator.return != null) {
  159. _iterator.return();
  160. }
  161. } finally {
  162. if (_didIteratorError) {
  163. throw _iteratorError;
  164. }
  165. }
  166. }
  167. }
  168. function worker(_x) {
  169. return _worker.apply(this, arguments);
  170. }
  171. function _worker() {
  172. _worker = _asyncToGenerator(function*({config, globalConfig, path, context}) {
  173. try {
  174. return yield (0, _runTest.default)(
  175. path,
  176. globalConfig,
  177. config,
  178. getResolver(config),
  179. context &&
  180. _objectSpread({}, context, {
  181. changedFiles: context.changedFiles && new Set(context.changedFiles)
  182. })
  183. );
  184. } catch (error) {
  185. throw formatError(error);
  186. }
  187. });
  188. return _worker.apply(this, arguments);
  189. }