utils.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.resolveWatchPlugin =
  6. exports.resolveTestEnvironment =
  7. exports.resolveSequencer =
  8. exports.resolveRunner =
  9. void 0;
  10. function path() {
  11. const data = _interopRequireWildcard(require('path'));
  12. path = function () {
  13. return data;
  14. };
  15. return data;
  16. }
  17. function _chalk() {
  18. const data = _interopRequireDefault(require('chalk'));
  19. _chalk = function () {
  20. return data;
  21. };
  22. return data;
  23. }
  24. function _jestValidate() {
  25. const data = require('jest-validate');
  26. _jestValidate = function () {
  27. return data;
  28. };
  29. return data;
  30. }
  31. var _resolver = _interopRequireDefault(require('./resolver'));
  32. function _interopRequireDefault(obj) {
  33. return obj && obj.__esModule ? obj : {default: obj};
  34. }
  35. function _getRequireWildcardCache(nodeInterop) {
  36. if (typeof WeakMap !== 'function') return null;
  37. var cacheBabelInterop = new WeakMap();
  38. var cacheNodeInterop = new WeakMap();
  39. return (_getRequireWildcardCache = function (nodeInterop) {
  40. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  41. })(nodeInterop);
  42. }
  43. function _interopRequireWildcard(obj, nodeInterop) {
  44. if (!nodeInterop && obj && obj.__esModule) {
  45. return obj;
  46. }
  47. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  48. return {default: obj};
  49. }
  50. var cache = _getRequireWildcardCache(nodeInterop);
  51. if (cache && cache.has(obj)) {
  52. return cache.get(obj);
  53. }
  54. var newObj = {};
  55. var hasPropertyDescriptor =
  56. Object.defineProperty && Object.getOwnPropertyDescriptor;
  57. for (var key in obj) {
  58. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  59. var desc = hasPropertyDescriptor
  60. ? Object.getOwnPropertyDescriptor(obj, key)
  61. : null;
  62. if (desc && (desc.get || desc.set)) {
  63. Object.defineProperty(newObj, key, desc);
  64. } else {
  65. newObj[key] = obj[key];
  66. }
  67. }
  68. }
  69. newObj.default = obj;
  70. if (cache) {
  71. cache.set(obj, newObj);
  72. }
  73. return newObj;
  74. }
  75. /**
  76. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  77. *
  78. * This source code is licensed under the MIT license found in the
  79. * LICENSE file in the root directory of this source tree.
  80. */
  81. const BULLET = _chalk().default.bold('\u25cf ');
  82. const DOCUMENTATION_NOTE = ` ${_chalk().default.bold(
  83. 'Configuration Documentation:'
  84. )}
  85. https://jestjs.io/docs/configuration
  86. `;
  87. const createValidationError = message =>
  88. new (_jestValidate().ValidationError)(
  89. `${BULLET}Validation Error`,
  90. message,
  91. DOCUMENTATION_NOTE
  92. );
  93. const replaceRootDirInPath = (rootDir, filePath) => {
  94. if (!/^<rootDir>/.test(filePath)) {
  95. return filePath;
  96. }
  97. return path().resolve(
  98. rootDir,
  99. path().normalize('./' + filePath.substr('<rootDir>'.length))
  100. );
  101. };
  102. const resolveWithPrefix = (
  103. resolver,
  104. {
  105. filePath,
  106. humanOptionName,
  107. optionName,
  108. prefix,
  109. requireResolveFunction,
  110. rootDir
  111. }
  112. ) => {
  113. const fileName = replaceRootDirInPath(rootDir, filePath);
  114. let module = _resolver.default.findNodeModule(`${prefix}${fileName}`, {
  115. basedir: rootDir,
  116. resolver: resolver || undefined
  117. });
  118. if (module) {
  119. return module;
  120. }
  121. try {
  122. return requireResolveFunction(`${prefix}${fileName}`);
  123. } catch {}
  124. module = _resolver.default.findNodeModule(fileName, {
  125. basedir: rootDir,
  126. resolver: resolver || undefined
  127. });
  128. if (module) {
  129. return module;
  130. }
  131. try {
  132. return requireResolveFunction(fileName);
  133. } catch {}
  134. throw createValidationError(
  135. ` ${humanOptionName} ${_chalk().default.bold(
  136. fileName
  137. )} cannot be found. Make sure the ${_chalk().default.bold(
  138. optionName
  139. )} configuration option points to an existing node module.`
  140. );
  141. };
  142. /**
  143. * Finds the test environment to use:
  144. *
  145. * 1. looks for jest-environment-<name> relative to project.
  146. * 1. looks for jest-environment-<name> relative to Jest.
  147. * 1. looks for <name> relative to project.
  148. * 1. looks for <name> relative to Jest.
  149. */
  150. const resolveTestEnvironment = ({
  151. rootDir,
  152. testEnvironment: filePath,
  153. // TODO: remove default in Jest 28
  154. requireResolveFunction = require.resolve
  155. }) =>
  156. resolveWithPrefix(undefined, {
  157. filePath,
  158. humanOptionName: 'Test environment',
  159. optionName: 'testEnvironment',
  160. prefix: 'jest-environment-',
  161. requireResolveFunction,
  162. rootDir
  163. });
  164. /**
  165. * Finds the watch plugins to use:
  166. *
  167. * 1. looks for jest-watch-<name> relative to project.
  168. * 1. looks for jest-watch-<name> relative to Jest.
  169. * 1. looks for <name> relative to project.
  170. * 1. looks for <name> relative to Jest.
  171. */
  172. exports.resolveTestEnvironment = resolveTestEnvironment;
  173. const resolveWatchPlugin = (
  174. resolver,
  175. {
  176. filePath,
  177. rootDir,
  178. // TODO: remove default in Jest 28
  179. requireResolveFunction = require.resolve
  180. }
  181. ) =>
  182. resolveWithPrefix(resolver, {
  183. filePath,
  184. humanOptionName: 'Watch plugin',
  185. optionName: 'watchPlugins',
  186. prefix: 'jest-watch-',
  187. requireResolveFunction,
  188. rootDir
  189. });
  190. /**
  191. * Finds the runner to use:
  192. *
  193. * 1. looks for jest-runner-<name> relative to project.
  194. * 1. looks for jest-runner-<name> relative to Jest.
  195. * 1. looks for <name> relative to project.
  196. * 1. looks for <name> relative to Jest.
  197. */
  198. exports.resolveWatchPlugin = resolveWatchPlugin;
  199. const resolveRunner = (
  200. resolver,
  201. {
  202. filePath,
  203. rootDir,
  204. // TODO: remove default in Jest 28
  205. requireResolveFunction = require.resolve
  206. }
  207. ) =>
  208. resolveWithPrefix(resolver, {
  209. filePath,
  210. humanOptionName: 'Jest Runner',
  211. optionName: 'runner',
  212. prefix: 'jest-runner-',
  213. requireResolveFunction,
  214. rootDir
  215. });
  216. exports.resolveRunner = resolveRunner;
  217. const resolveSequencer = (
  218. resolver,
  219. {
  220. filePath,
  221. rootDir,
  222. // TODO: remove default in Jest 28
  223. requireResolveFunction = require.resolve
  224. }
  225. ) =>
  226. resolveWithPrefix(resolver, {
  227. filePath,
  228. humanOptionName: 'Jest Sequencer',
  229. optionName: 'testSequencer',
  230. prefix: 'jest-sequencer-',
  231. requireResolveFunction,
  232. rootDir
  233. });
  234. exports.resolveSequencer = resolveSequencer;