utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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) Facebook, Inc. and its affiliates. All Rights Reserved.
  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. } /// can cast as string since nulls will be thrown
  118. return module;
  119. };
  120. exports.resolve = resolve;
  121. const escapeGlobCharacters = path => path.replace(/([()*{}\[\]!?\\])/g, '\\$1');
  122. exports.escapeGlobCharacters = escapeGlobCharacters;
  123. const replaceRootDirInPath = (rootDir, filePath) => {
  124. if (!/^<rootDir>/.test(filePath)) {
  125. return filePath;
  126. }
  127. return path().resolve(
  128. rootDir,
  129. path().normalize('./' + filePath.substring('<rootDir>'.length))
  130. );
  131. };
  132. exports.replaceRootDirInPath = replaceRootDirInPath;
  133. const _replaceRootDirInObject = (rootDir, config) => {
  134. const newConfig = {};
  135. for (const configKey in config) {
  136. newConfig[configKey] =
  137. configKey === 'rootDir'
  138. ? config[configKey]
  139. : _replaceRootDirTags(rootDir, config[configKey]);
  140. }
  141. return newConfig;
  142. };
  143. const _replaceRootDirTags = (rootDir, config) => {
  144. if (config == null) {
  145. return config;
  146. }
  147. switch (typeof config) {
  148. case 'object':
  149. if (Array.isArray(config)) {
  150. /// can be string[] or {}[]
  151. return config.map(item => _replaceRootDirTags(rootDir, item));
  152. }
  153. if (config instanceof RegExp) {
  154. return config;
  155. }
  156. return _replaceRootDirInObject(rootDir, config);
  157. case 'string':
  158. return replaceRootDirInPath(rootDir, config);
  159. }
  160. return config;
  161. };
  162. exports._replaceRootDirTags = _replaceRootDirTags;
  163. // newtype
  164. const isJSONString = text =>
  165. text != null &&
  166. typeof text === 'string' &&
  167. text.startsWith('{') &&
  168. text.endsWith('}');
  169. exports.isJSONString = isJSONString;