resolveConfigPath.js 6.3 KB

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