runGlobalHook.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = runGlobalHook;
  6. function util() {
  7. const data = _interopRequireWildcard(require('util'));
  8. util = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _transform() {
  14. const data = require('@jest/transform');
  15. _transform = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _prettyFormat() {
  21. const data = _interopRequireDefault(require('pretty-format'));
  22. _prettyFormat = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _interopRequireDefault(obj) {
  28. return obj && obj.__esModule ? obj : {default: obj};
  29. }
  30. function _getRequireWildcardCache(nodeInterop) {
  31. if (typeof WeakMap !== 'function') return null;
  32. var cacheBabelInterop = new WeakMap();
  33. var cacheNodeInterop = new WeakMap();
  34. return (_getRequireWildcardCache = function (nodeInterop) {
  35. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  36. })(nodeInterop);
  37. }
  38. function _interopRequireWildcard(obj, nodeInterop) {
  39. if (!nodeInterop && obj && obj.__esModule) {
  40. return obj;
  41. }
  42. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  43. return {default: obj};
  44. }
  45. var cache = _getRequireWildcardCache(nodeInterop);
  46. if (cache && cache.has(obj)) {
  47. return cache.get(obj);
  48. }
  49. var newObj = {};
  50. var hasPropertyDescriptor =
  51. Object.defineProperty && Object.getOwnPropertyDescriptor;
  52. for (var key in obj) {
  53. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  54. var desc = hasPropertyDescriptor
  55. ? Object.getOwnPropertyDescriptor(obj, key)
  56. : null;
  57. if (desc && (desc.get || desc.set)) {
  58. Object.defineProperty(newObj, key, desc);
  59. } else {
  60. newObj[key] = obj[key];
  61. }
  62. }
  63. }
  64. newObj.default = obj;
  65. if (cache) {
  66. cache.set(obj, newObj);
  67. }
  68. return newObj;
  69. }
  70. /**
  71. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  72. *
  73. * This source code is licensed under the MIT license found in the
  74. * LICENSE file in the root directory of this source tree.
  75. */
  76. async function runGlobalHook({allTests, globalConfig, moduleName}) {
  77. const globalModulePaths = new Set(
  78. allTests.map(test => test.context.config[moduleName])
  79. );
  80. if (globalConfig[moduleName]) {
  81. globalModulePaths.add(globalConfig[moduleName]);
  82. }
  83. if (globalModulePaths.size > 0) {
  84. for (const modulePath of globalModulePaths) {
  85. if (!modulePath) {
  86. continue;
  87. }
  88. const correctConfig = allTests.find(
  89. t => t.context.config[moduleName] === modulePath
  90. );
  91. const projectConfig = correctConfig
  92. ? correctConfig.context.config // Fallback to first config
  93. : allTests[0].context.config;
  94. const transformer = await (0, _transform().createScriptTransformer)(
  95. projectConfig
  96. );
  97. try {
  98. await transformer.requireAndTranspileModule(
  99. modulePath,
  100. async globalModule => {
  101. if (typeof globalModule !== 'function') {
  102. throw new TypeError(
  103. `${moduleName} file must export a function at ${modulePath}`
  104. );
  105. }
  106. await globalModule(globalConfig);
  107. }
  108. );
  109. } catch (error) {
  110. if (util().types.isNativeError(error)) {
  111. error.message = `Jest: Got error running ${moduleName} - ${modulePath}, reason: ${error.message}`;
  112. throw error;
  113. }
  114. throw new Error(
  115. `Jest: Got error running ${moduleName} - ${modulePath}, reason: ${(0,
  116. _prettyFormat().default)(error, {
  117. maxDepth: 3
  118. })}`
  119. );
  120. }
  121. }
  122. }
  123. }