utils.js 7.4 KB

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