readConfigFileAndSetRootDir.js 5.6 KB

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