testWorker.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.setup = setup;
  6. exports.worker = worker;
  7. function _exit() {
  8. const data = _interopRequireDefault(require('exit'));
  9. _exit = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _jestHasteMap() {
  15. const data = _interopRequireDefault(require('jest-haste-map'));
  16. _jestHasteMap = function () {
  17. return data;
  18. };
  19. return data;
  20. }
  21. function _jestMessageUtil() {
  22. const data = require('jest-message-util');
  23. _jestMessageUtil = function () {
  24. return data;
  25. };
  26. return data;
  27. }
  28. function _jestRuntime() {
  29. const data = _interopRequireDefault(require('jest-runtime'));
  30. _jestRuntime = function () {
  31. return data;
  32. };
  33. return data;
  34. }
  35. function _jestWorker() {
  36. const data = require('jest-worker');
  37. _jestWorker = function () {
  38. return data;
  39. };
  40. return data;
  41. }
  42. var _runTest = _interopRequireDefault(require('./runTest'));
  43. function _interopRequireDefault(obj) {
  44. return obj && obj.__esModule ? obj : {default: obj};
  45. }
  46. /**
  47. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  48. *
  49. * This source code is licensed under the MIT license found in the
  50. * LICENSE file in the root directory of this source tree.
  51. *
  52. */
  53. // Make sure uncaught errors are logged before we exit.
  54. process.on('uncaughtException', err => {
  55. console.error(err.stack);
  56. (0, _exit().default)(1);
  57. });
  58. const formatError = error => {
  59. if (typeof error === 'string') {
  60. const {message, stack} = (0, _jestMessageUtil().separateMessageFromStack)(
  61. error
  62. );
  63. return {
  64. message,
  65. stack,
  66. type: 'Error'
  67. };
  68. }
  69. return {
  70. code: error.code || undefined,
  71. message: error.message,
  72. stack: error.stack,
  73. type: 'Error'
  74. };
  75. };
  76. const resolvers = new Map();
  77. const getResolver = config => {
  78. const resolver = resolvers.get(config.name);
  79. if (!resolver) {
  80. throw new Error('Cannot find resolver for: ' + config.name);
  81. }
  82. return resolver;
  83. };
  84. function setup(setupData) {
  85. // Module maps that will be needed for the test runs are passed.
  86. for (const {
  87. config,
  88. serializableModuleMap
  89. } of setupData.serializableResolvers) {
  90. const moduleMap = _jestHasteMap().default.ModuleMap.fromJSON(
  91. serializableModuleMap
  92. );
  93. resolvers.set(
  94. config.name,
  95. _jestRuntime().default.createResolver(config, moduleMap)
  96. );
  97. }
  98. }
  99. const sendMessageToJest = (eventName, args) => {
  100. (0, _jestWorker().messageParent)([eventName, args]);
  101. };
  102. async function worker({config, globalConfig, path, context}) {
  103. try {
  104. return await (0, _runTest.default)(
  105. path,
  106. globalConfig,
  107. config,
  108. getResolver(config),
  109. context && {
  110. ...context,
  111. changedFiles: context.changedFiles && new Set(context.changedFiles),
  112. sourcesRelatedToTestsInChangedFiles:
  113. context.sourcesRelatedToTestsInChangedFiles &&
  114. new Set(context.sourcesRelatedToTestsInChangedFiles)
  115. },
  116. sendMessageToJest
  117. );
  118. } catch (error) {
  119. throw formatError(error);
  120. }
  121. }