UNPKG

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