readConfigFileAndSetRootDir.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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) Facebook, Inc. and its affiliates. All Rights Reserved.
  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.rootDir = path().resolve(
  134. path().dirname(configPath),
  135. configObject.rootDir
  136. );
  137. }
  138. } else {
  139. // If rootDir is not there, we'll set it to this file's __dirname
  140. configObject.rootDir = path().dirname(configPath);
  141. }
  142. return configObject;
  143. }
  144. let registerer; // Load the TypeScript configuration
  145. const loadTSConfigFile = async configPath => {
  146. // Register TypeScript compiler instance
  147. try {
  148. registerer ||
  149. (registerer = require('ts-node').register({
  150. compilerOptions: {
  151. module: 'CommonJS'
  152. }
  153. }));
  154. } catch (e) {
  155. if (e.code === 'MODULE_NOT_FOUND') {
  156. throw new Error(
  157. `Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${e.message}`
  158. );
  159. }
  160. throw e;
  161. }
  162. registerer.enabled(true);
  163. let configObject = (0, _jestUtil().interopRequireDefault)(
  164. require(configPath)
  165. ).default; // In case the config is a function which imports more Typescript code
  166. if (typeof configObject === 'function') {
  167. configObject = await configObject();
  168. }
  169. registerer.enabled(false);
  170. return configObject;
  171. };