utils.js 6.4 KB

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