utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.resolve =
  6. exports.replaceRootDirInPath =
  7. exports.isJSONString =
  8. exports.escapeGlobCharacters =
  9. exports._replaceRootDirTags =
  10. exports.DOCUMENTATION_NOTE =
  11. exports.BULLET =
  12. void 0;
  13. function path() {
  14. const data = _interopRequireWildcard(require('path'));
  15. path = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _chalk() {
  21. const data = _interopRequireDefault(require('chalk'));
  22. _chalk = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _jestResolve() {
  28. const data = _interopRequireDefault(require('jest-resolve'));
  29. _jestResolve = 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. function _interopRequireDefault(obj) {
  42. return obj && obj.__esModule ? obj : {default: obj};
  43. }
  44. function _getRequireWildcardCache(nodeInterop) {
  45. if (typeof WeakMap !== 'function') return null;
  46. var cacheBabelInterop = new WeakMap();
  47. var cacheNodeInterop = new WeakMap();
  48. return (_getRequireWildcardCache = function (nodeInterop) {
  49. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  50. })(nodeInterop);
  51. }
  52. function _interopRequireWildcard(obj, nodeInterop) {
  53. if (!nodeInterop && obj && obj.__esModule) {
  54. return obj;
  55. }
  56. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  57. return {default: obj};
  58. }
  59. var cache = _getRequireWildcardCache(nodeInterop);
  60. if (cache && cache.has(obj)) {
  61. return cache.get(obj);
  62. }
  63. var newObj = {};
  64. var hasPropertyDescriptor =
  65. Object.defineProperty && Object.getOwnPropertyDescriptor;
  66. for (var key in obj) {
  67. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  68. var desc = hasPropertyDescriptor
  69. ? Object.getOwnPropertyDescriptor(obj, key)
  70. : null;
  71. if (desc && (desc.get || desc.set)) {
  72. Object.defineProperty(newObj, key, desc);
  73. } else {
  74. newObj[key] = obj[key];
  75. }
  76. }
  77. }
  78. newObj.default = obj;
  79. if (cache) {
  80. cache.set(obj, newObj);
  81. }
  82. return newObj;
  83. }
  84. /**
  85. * Copyright (c) Meta Platforms, Inc. and affiliates.
  86. *
  87. * This source code is licensed under the MIT license found in the
  88. * LICENSE file in the root directory of this source tree.
  89. */
  90. const BULLET = _chalk().default.bold('\u25cf ');
  91. exports.BULLET = BULLET;
  92. const DOCUMENTATION_NOTE = ` ${_chalk().default.bold(
  93. 'Configuration Documentation:'
  94. )}
  95. https://jestjs.io/docs/configuration
  96. `;
  97. exports.DOCUMENTATION_NOTE = DOCUMENTATION_NOTE;
  98. const createValidationError = message =>
  99. new (_jestValidate().ValidationError)(
  100. `${BULLET}Validation Error`,
  101. message,
  102. DOCUMENTATION_NOTE
  103. );
  104. const resolve = (resolver, {key, filePath, rootDir, optional}) => {
  105. const module = _jestResolve().default.findNodeModule(
  106. replaceRootDirInPath(rootDir, filePath),
  107. {
  108. basedir: rootDir,
  109. resolver: resolver || undefined
  110. }
  111. );
  112. if (!module && !optional) {
  113. throw createValidationError(` Module ${_chalk().default.bold(
  114. filePath
  115. )} in the ${_chalk().default.bold(key)} option was not found.
  116. ${_chalk().default.bold('<rootDir>')} is: ${rootDir}`);
  117. }
  118. /// can cast as string since nulls will be thrown
  119. return module;
  120. };
  121. exports.resolve = resolve;
  122. const escapeGlobCharacters = path => path.replace(/([()*{}[\]!?\\])/g, '\\$1');
  123. exports.escapeGlobCharacters = escapeGlobCharacters;
  124. const replaceRootDirInPath = (rootDir, filePath) => {
  125. if (!/^<rootDir>/.test(filePath)) {
  126. return filePath;
  127. }
  128. return path().resolve(
  129. rootDir,
  130. path().normalize(`./${filePath.substring('<rootDir>'.length)}`)
  131. );
  132. };
  133. exports.replaceRootDirInPath = replaceRootDirInPath;
  134. const _replaceRootDirInObject = (rootDir, config) => {
  135. const newConfig = {};
  136. for (const configKey in config) {
  137. newConfig[configKey] =
  138. configKey === 'rootDir'
  139. ? config[configKey]
  140. : _replaceRootDirTags(rootDir, config[configKey]);
  141. }
  142. return newConfig;
  143. };
  144. const _replaceRootDirTags = (rootDir, config) => {
  145. if (config == null) {
  146. return config;
  147. }
  148. switch (typeof config) {
  149. case 'object':
  150. if (Array.isArray(config)) {
  151. /// can be string[] or {}[]
  152. return config.map(item => _replaceRootDirTags(rootDir, item));
  153. }
  154. if (config instanceof RegExp) {
  155. return config;
  156. }
  157. return _replaceRootDirInObject(rootDir, config);
  158. case 'string':
  159. return replaceRootDirInPath(rootDir, config);
  160. }
  161. return config;
  162. };
  163. exports._replaceRootDirTags = _replaceRootDirTags;
  164. // newtype
  165. const isJSONString = text =>
  166. text != null &&
  167. typeof text === 'string' &&
  168. text.startsWith('{') &&
  169. text.endsWith('}');
  170. exports.isJSONString = isJSONString;