readConfigFileAndSetRootDir.js
1 'use strict'; 2 3 Object.defineProperty(exports, '__esModule', { 4 value: true 5 }); 6 exports.default = readConfigFileAndSetRootDir; 7 8 function path() { 9 const data = _interopRequireWildcard(require('path')); 10 11 path = function () { 12 return data; 13 }; 14 15 return data; 16 } 17 18 function _url() { 19 const data = require('url'); 20 21 _url = function () { 22 return data; 23 }; 24 25 return data; 26 } 27 28 function fs() { 29 const data = _interopRequireWildcard(require('graceful-fs')); 30 31 fs = function () { 32 return data; 33 }; 34 35 return data; 36 } 37 38 function _jestUtil() { 39 const data = require('jest-util'); 40 41 _jestUtil = function () { 42 return data; 43 }; 44 45 return data; 46 } 47 48 var _constants = require('./constants'); 49 50 var _jsonlint = _interopRequireDefault(require('./vendor/jsonlint')); 51 52 function _interopRequireDefault(obj) { 53 return obj && obj.__esModule ? obj : {default: obj}; 54 } 55 56 function _getRequireWildcardCache() { 57 if (typeof WeakMap !== 'function') return null; 58 var cache = new WeakMap(); 59 _getRequireWildcardCache = function () { 60 return cache; 61 }; 62 return cache; 63 } 64 65 function _interopRequireWildcard(obj) { 66 if (obj && obj.__esModule) { 67 return obj; 68 } 69 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) { 70 return {default: obj}; 71 } 72 var cache = _getRequireWildcardCache(); 73 if (cache && cache.has(obj)) { 74 return cache.get(obj); 75 } 76 var newObj = {}; 77 var hasPropertyDescriptor = 78 Object.defineProperty && Object.getOwnPropertyDescriptor; 79 for (var key in obj) { 80 if (Object.prototype.hasOwnProperty.call(obj, key)) { 81 var desc = hasPropertyDescriptor 82 ? Object.getOwnPropertyDescriptor(obj, key) 83 : null; 84 if (desc && (desc.get || desc.set)) { 85 Object.defineProperty(newObj, key, desc); 86 } else { 87 newObj[key] = obj[key]; 88 } 89 } 90 } 91 newObj.default = obj; 92 if (cache) { 93 cache.set(obj, newObj); 94 } 95 return newObj; 96 } 97 98 /** 99 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 100 * 101 * This source code is licensed under the MIT license found in the 102 * LICENSE file in the root directory of this source tree. 103 */ 104 // @ts-expect-error: vendored 105 // Read the configuration and set its `rootDir` 106 // 1. If it's a `package.json` file, we look into its "jest" property 107 // 2. If it's a `jest.config.ts` file, we use `ts-node` to transpile & require it 108 // 3. For any other file, we just require it. If we receive an 'ERR_REQUIRE_ESM' 109 // from node, perform a dynamic import instead. 110 async function readConfigFileAndSetRootDir(configPath) { 111 const isTS = configPath.endsWith(_constants.JEST_CONFIG_EXT_TS); 112 const isJSON = configPath.endsWith(_constants.JEST_CONFIG_EXT_JSON); 113 let configObject; 114 115 try { 116 if (isTS) { 117 configObject = await loadTSConfigFile(configPath); 118 } else { 119 configObject = require(configPath); 120 } 121 } catch (error) { 122 if (error.code === 'ERR_REQUIRE_ESM') { 123 try { 124 const configUrl = (0, _url().pathToFileURL)(configPath); // node `import()` supports URL, but TypeScript doesn't know that 125 126 const importedConfig = await import(configUrl.href); 127 128 if (!importedConfig.default) { 129 throw new Error( 130 `Jest: Failed to load mjs config file ${configPath} - did you use a default export?` 131 ); 132 } 133 134 configObject = importedConfig.default; 135 } catch (innerError) { 136 if (innerError.message === 'Not supported') { 137 throw new Error( 138 `Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension for file ${configPath}` 139 ); 140 } 141 142 throw innerError; 143 } 144 } else if (isJSON) { 145 throw new Error( 146 `Jest: Failed to parse config file ${configPath}\n` + 147 ` ${_jsonlint.default.errors(fs().readFileSync(configPath, 'utf8'))}` 148 ); 149 } else if (isTS) { 150 throw new Error( 151 `Jest: Failed to parse the TypeScript config file ${configPath}\n` + 152 ` ${error}` 153 ); 154 } else { 155 throw error; 156 } 157 } 158 159 if (configPath.endsWith(_constants.PACKAGE_JSON)) { 160 // Event if there's no "jest" property in package.json we will still use 161 // an empty object. 162 configObject = configObject.jest || {}; 163 } 164 165 if (configObject.rootDir) { 166 // We don't touch it if it has an absolute path specified 167 if (!path().isAbsolute(configObject.rootDir)) { 168 // otherwise, we'll resolve it relative to the file's __dirname 169 configObject.rootDir = path().resolve( 170 path().dirname(configPath), 171 configObject.rootDir 172 ); 173 } 174 } else { 175 // If rootDir is not there, we'll set it to this file's __dirname 176 configObject.rootDir = path().dirname(configPath); 177 } 178 179 return configObject; 180 } // Load the TypeScript configuration 181 182 const loadTSConfigFile = async configPath => { 183 let registerer; // Register TypeScript compiler instance 184 185 try { 186 registerer = require('ts-node').register({ 187 compilerOptions: { 188 module: 'CommonJS' 189 } 190 }); 191 } catch (e) { 192 if (e.code === 'MODULE_NOT_FOUND') { 193 throw new Error( 194 `Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${e.message}` 195 ); 196 } 197 198 throw e; 199 } 200 201 registerer.enabled(true); 202 let configObject = (0, _jestUtil().interopRequireDefault)(require(configPath)) 203 .default; // In case the config is a function which imports more Typescript code 204 205 if (typeof configObject === 'function') { 206 configObject = await configObject(); 207 } 208 209 registerer.enabled(false); 210 return configObject; 211 };