resolveConfigPath.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function path() {
  7. const data = _interopRequireWildcard(require('path'));
  8. path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _chalk() {
  14. const data = _interopRequireDefault(require('chalk'));
  15. _chalk = 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 _slash() {
  28. const data = _interopRequireDefault(require('slash'));
  29. _slash = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. var _constants = require('./constants');
  35. function _interopRequireDefault(obj) {
  36. return obj && obj.__esModule ? obj : {default: obj};
  37. }
  38. function _getRequireWildcardCache(nodeInterop) {
  39. if (typeof WeakMap !== 'function') return null;
  40. var cacheBabelInterop = new WeakMap();
  41. var cacheNodeInterop = new WeakMap();
  42. return (_getRequireWildcardCache = function (nodeInterop) {
  43. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  44. })(nodeInterop);
  45. }
  46. function _interopRequireWildcard(obj, nodeInterop) {
  47. if (!nodeInterop && obj && obj.__esModule) {
  48. return obj;
  49. }
  50. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  51. return {default: obj};
  52. }
  53. var cache = _getRequireWildcardCache(nodeInterop);
  54. if (cache && cache.has(obj)) {
  55. return cache.get(obj);
  56. }
  57. var newObj = {};
  58. var hasPropertyDescriptor =
  59. Object.defineProperty && Object.getOwnPropertyDescriptor;
  60. for (var key in obj) {
  61. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  62. var desc = hasPropertyDescriptor
  63. ? Object.getOwnPropertyDescriptor(obj, key)
  64. : null;
  65. if (desc && (desc.get || desc.set)) {
  66. Object.defineProperty(newObj, key, desc);
  67. } else {
  68. newObj[key] = obj[key];
  69. }
  70. }
  71. }
  72. newObj.default = obj;
  73. if (cache) {
  74. cache.set(obj, newObj);
  75. }
  76. return newObj;
  77. }
  78. /**
  79. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  80. *
  81. * This source code is licensed under the MIT license found in the
  82. * LICENSE file in the root directory of this source tree.
  83. */
  84. const isFile = filePath =>
  85. fs().existsSync(filePath) && !fs().lstatSync(filePath).isDirectory();
  86. const getConfigFilename = ext => _constants.JEST_CONFIG_BASE_NAME + ext;
  87. var _default = (pathToResolve, cwd, skipMultipleConfigWarning = false) => {
  88. if (!path().isAbsolute(cwd)) {
  89. throw new Error(`"cwd" must be an absolute path. cwd: ${cwd}`);
  90. }
  91. const absolutePath = path().isAbsolute(pathToResolve)
  92. ? pathToResolve
  93. : path().resolve(cwd, pathToResolve);
  94. if (isFile(absolutePath)) {
  95. return absolutePath;
  96. } // This is a guard against passing non existing path as a project/config,
  97. // that will otherwise result in a very confusing situation.
  98. // e.g.
  99. // With a directory structure like this:
  100. // my_project/
  101. // packcage.json
  102. //
  103. // Passing a `my_project/some_directory_that_doesnt_exist` as a project
  104. // name will resolve into a (possibly empty) `my_project/package.json` and
  105. // try to run all tests it finds under `my_project` directory.
  106. if (!fs().existsSync(absolutePath)) {
  107. throw new Error(
  108. `Can't find a root directory while resolving a config file path.\n` +
  109. `Provided path to resolve: ${pathToResolve}\n` +
  110. `cwd: ${cwd}`
  111. );
  112. }
  113. return resolveConfigPathByTraversing(
  114. absolutePath,
  115. pathToResolve,
  116. cwd,
  117. skipMultipleConfigWarning
  118. );
  119. };
  120. exports.default = _default;
  121. const resolveConfigPathByTraversing = (
  122. pathToResolve,
  123. initialPath,
  124. cwd,
  125. skipMultipleConfigWarning
  126. ) => {
  127. const configFiles = _constants.JEST_CONFIG_EXT_ORDER.map(ext =>
  128. path().resolve(pathToResolve, getConfigFilename(ext))
  129. ).filter(isFile);
  130. const packageJson = findPackageJson(pathToResolve);
  131. if (packageJson && hasPackageJsonJestKey(packageJson)) {
  132. configFiles.push(packageJson);
  133. }
  134. if (!skipMultipleConfigWarning && configFiles.length > 1) {
  135. console.warn(makeMultipleConfigsWarning(configFiles));
  136. }
  137. if (configFiles.length > 0 || packageJson) {
  138. var _configFiles$;
  139. return (_configFiles$ = configFiles[0]) !== null && _configFiles$ !== void 0
  140. ? _configFiles$
  141. : packageJson;
  142. } // This is the system root.
  143. // We tried everything, config is nowhere to be found ¯\_(ツ)_/¯
  144. if (pathToResolve === path().dirname(pathToResolve)) {
  145. throw new Error(makeResolutionErrorMessage(initialPath, cwd));
  146. } // go up a level and try it again
  147. return resolveConfigPathByTraversing(
  148. path().dirname(pathToResolve),
  149. initialPath,
  150. cwd,
  151. skipMultipleConfigWarning
  152. );
  153. };
  154. const findPackageJson = pathToResolve => {
  155. const packagePath = path().resolve(pathToResolve, _constants.PACKAGE_JSON);
  156. if (isFile(packagePath)) {
  157. return packagePath;
  158. }
  159. return undefined;
  160. };
  161. const hasPackageJsonJestKey = packagePath => {
  162. const content = fs().readFileSync(packagePath, 'utf8');
  163. try {
  164. return 'jest' in JSON.parse(content);
  165. } catch {
  166. // If package is not a valid JSON
  167. return false;
  168. }
  169. };
  170. const makeResolutionErrorMessage = (initialPath, cwd) =>
  171. 'Could not find a config file based on provided values:\n' +
  172. `path: "${initialPath}"\n` +
  173. `cwd: "${cwd}"\n` +
  174. 'Config paths must be specified by either a direct path to a config\n' +
  175. 'file, or a path to a directory. If directory is given, Jest will try to\n' +
  176. `traverse directory tree up, until it finds one of those files in exact order: ${_constants.JEST_CONFIG_EXT_ORDER.map(
  177. ext => `"${getConfigFilename(ext)}"`
  178. ).join(' or ')}.`;
  179. function extraIfPackageJson(configPath) {
  180. if (configPath.endsWith(_constants.PACKAGE_JSON)) {
  181. return '`jest` key in ';
  182. }
  183. return '';
  184. }
  185. const makeMultipleConfigsWarning = configPaths =>
  186. _chalk().default.yellow(
  187. [
  188. _chalk().default.bold('\u25cf Multiple configurations found:'),
  189. ...configPaths.map(
  190. configPath =>
  191. ` * ${extraIfPackageJson(configPath)}${(0, _slash().default)(
  192. configPath
  193. )}`
  194. ),
  195. '',
  196. ' Implicit config resolution does not allow multiple configuration files.',
  197. ' Either remove unused config files or select one explicitly with `--config`.',
  198. '',
  199. ' Configuration Documentation:',
  200. ' https://jestjs.io/docs/configuration.html',
  201. ''
  202. ].join('\n')
  203. );