readConfigFileAndSetRootDir.js 5.5 KB

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