resolveConfigPath.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. 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. function resolveConfigPath(
  88. pathToResolve,
  89. cwd,
  90. skipMultipleConfigWarning = false
  91. ) {
  92. if (!path().isAbsolute(cwd)) {
  93. throw new Error(`"cwd" must be an absolute path. cwd: ${cwd}`);
  94. }
  95. const absolutePath = path().isAbsolute(pathToResolve)
  96. ? pathToResolve
  97. : path().resolve(cwd, pathToResolve);
  98. if (isFile(absolutePath)) {
  99. return absolutePath;
  100. } // This is a guard against passing non existing path as a project/config,
  101. // that will otherwise result in a very confusing situation.
  102. // e.g.
  103. // With a directory structure like this:
  104. // my_project/
  105. // package.json
  106. //
  107. // Passing a `my_project/some_directory_that_doesnt_exist` as a project
  108. // name will resolve into a (possibly empty) `my_project/package.json` and
  109. // try to run all tests it finds under `my_project` directory.
  110. if (!fs().existsSync(absolutePath)) {
  111. throw new Error(
  112. "Can't find a root directory while resolving a config file path.\n" +
  113. `Provided path to resolve: ${pathToResolve}\n` +
  114. `cwd: ${cwd}`
  115. );
  116. }
  117. return resolveConfigPathByTraversing(
  118. absolutePath,
  119. pathToResolve,
  120. cwd,
  121. skipMultipleConfigWarning
  122. );
  123. }
  124. const resolveConfigPathByTraversing = (
  125. pathToResolve,
  126. initialPath,
  127. cwd,
  128. skipMultipleConfigWarning
  129. ) => {
  130. const configFiles = _constants.JEST_CONFIG_EXT_ORDER.map(ext =>
  131. path().resolve(pathToResolve, getConfigFilename(ext))
  132. ).filter(isFile);
  133. const packageJson = findPackageJson(pathToResolve);
  134. if (packageJson && hasPackageJsonJestKey(packageJson)) {
  135. configFiles.push(packageJson);
  136. }
  137. if (!skipMultipleConfigWarning && configFiles.length > 1) {
  138. console.warn(makeMultipleConfigsWarning(configFiles));
  139. }
  140. if (configFiles.length > 0 || packageJson) {
  141. var _configFiles$;
  142. return (_configFiles$ = configFiles[0]) !== null && _configFiles$ !== void 0
  143. ? _configFiles$
  144. : packageJson;
  145. } // This is the system root.
  146. // We tried everything, config is nowhere to be found ¯\_(ツ)_/¯
  147. if (pathToResolve === path().dirname(pathToResolve)) {
  148. throw new Error(makeResolutionErrorMessage(initialPath, cwd));
  149. } // go up a level and try it again
  150. return resolveConfigPathByTraversing(
  151. path().dirname(pathToResolve),
  152. initialPath,
  153. cwd,
  154. skipMultipleConfigWarning
  155. );
  156. };
  157. const findPackageJson = pathToResolve => {
  158. const packagePath = path().resolve(pathToResolve, _constants.PACKAGE_JSON);
  159. if (isFile(packagePath)) {
  160. return packagePath;
  161. }
  162. return undefined;
  163. };
  164. const hasPackageJsonJestKey = packagePath => {
  165. const content = fs().readFileSync(packagePath, 'utf8');
  166. try {
  167. return 'jest' in JSON.parse(content);
  168. } catch {
  169. // If package is not a valid JSON
  170. return false;
  171. }
  172. };
  173. const makeResolutionErrorMessage = (initialPath, cwd) =>
  174. 'Could not find a config file based on provided values:\n' +
  175. `path: "${initialPath}"\n` +
  176. `cwd: "${cwd}"\n` +
  177. 'Config paths must be specified by either a direct path to a config\n' +
  178. 'file, or a path to a directory. If directory is given, Jest will try to\n' +
  179. `traverse directory tree up, until it finds one of those files in exact order: ${_constants.JEST_CONFIG_EXT_ORDER.map(
  180. ext => `"${getConfigFilename(ext)}"`
  181. ).join(' or ')}.`;
  182. function extraIfPackageJson(configPath) {
  183. if (configPath.endsWith(_constants.PACKAGE_JSON)) {
  184. return '`jest` key in ';
  185. }
  186. return '';
  187. }
  188. const makeMultipleConfigsWarning = configPaths =>
  189. _chalk().default.yellow(
  190. [
  191. _chalk().default.bold('\u25cf Multiple configurations found:'),
  192. ...configPaths.map(
  193. configPath =>
  194. ` * ${extraIfPackageJson(configPath)}${(0, _slash().default)(
  195. configPath
  196. )}`
  197. ),
  198. '',
  199. ' Implicit config resolution does not allow multiple configuration files.',
  200. ' Either remove unused config files or select one explicitly with `--config`.',
  201. '',
  202. ' Configuration Documentation:',
  203. ' https://jestjs.io/docs/configuration.html',
  204. ''
  205. ].join('\n')
  206. );