utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.isJSONString = exports.getTestEnvironment = exports._replaceRootDirTags = exports.replaceRootDirInPath = exports.escapeGlobCharacters = exports.resolve = exports.DOCUMENTATION_NOTE = exports.BULLET = undefined;
  6. var _path;
  7. function _load_path() {
  8. return (_path = _interopRequireDefault(require('path')));
  9. }
  10. var _jestValidate;
  11. function _load_jestValidate() {
  12. return (_jestValidate = require('jest-validate'));
  13. }
  14. var _jestResolve;
  15. function _load_jestResolve() {
  16. return (_jestResolve = _interopRequireDefault(require('jest-resolve')));
  17. }
  18. var _chalk;
  19. function _load_chalk() {
  20. return (_chalk = _interopRequireDefault(require('chalk')));
  21. }
  22. function _interopRequireDefault(obj) {
  23. return obj && obj.__esModule ? obj : {default: obj};
  24. }
  25. const BULLET = (exports.BULLET = (_chalk || _load_chalk()).default.bold(
  26. '\u25cf '
  27. ));
  28. /**
  29. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  30. *
  31. * This source code is licensed under the MIT license found in the
  32. * LICENSE file in the root directory of this source tree.
  33. *
  34. *
  35. */
  36. const DOCUMENTATION_NOTE = (exports.DOCUMENTATION_NOTE = ` ${(
  37. _chalk || _load_chalk()
  38. ).default.bold('Configuration Documentation:')}
  39. https://jestjs.io/docs/configuration.html
  40. `);
  41. const createValidationError = message =>
  42. new (_jestValidate || _load_jestValidate()).ValidationError(
  43. `${BULLET}Validation Error`,
  44. message,
  45. DOCUMENTATION_NOTE
  46. );
  47. const resolve = (exports.resolve = (resolver, _ref) => {
  48. let key = _ref.key,
  49. filePath = _ref.filePath,
  50. rootDir = _ref.rootDir,
  51. optional = _ref.optional;
  52. const module = (_jestResolve || _load_jestResolve()).default.findNodeModule(
  53. replaceRootDirInPath(rootDir, filePath),
  54. {
  55. basedir: rootDir,
  56. resolver: resolver
  57. }
  58. );
  59. if (!module && !optional) {
  60. throw createValidationError(` Module ${(
  61. _chalk || _load_chalk()
  62. ).default.bold(filePath)} in the ${(_chalk || _load_chalk()).default.bold(
  63. key
  64. )} option was not found.
  65. ${(_chalk || _load_chalk()).default.bold(
  66. '<rootDir>'
  67. )} is: ${rootDir}`);
  68. }
  69. return module;
  70. });
  71. const escapeGlobCharacters = (exports.escapeGlobCharacters = path =>
  72. path.replace(/([()*{}\[\]!?\\])/g, '\\$1'));
  73. const replaceRootDirInPath = (exports.replaceRootDirInPath = (
  74. rootDir,
  75. filePath
  76. ) => {
  77. if (!/^<rootDir>/.test(filePath)) {
  78. return filePath;
  79. }
  80. return (_path || _load_path()).default.resolve(
  81. rootDir,
  82. (_path || _load_path()).default.normalize(
  83. './' + filePath.substr('<rootDir>'.length)
  84. )
  85. );
  86. });
  87. const _replaceRootDirInObject = (rootDir, config) => {
  88. if (config !== null) {
  89. const newConfig = {};
  90. for (const configKey in config) {
  91. newConfig[configKey] =
  92. configKey === 'rootDir'
  93. ? config[configKey]
  94. : _replaceRootDirTags(rootDir, config[configKey]);
  95. }
  96. return newConfig;
  97. }
  98. return config;
  99. };
  100. const _replaceRootDirTags = (exports._replaceRootDirTags = (
  101. rootDir,
  102. config
  103. ) => {
  104. switch (typeof config) {
  105. case 'object':
  106. if (Array.isArray(config)) {
  107. return config.map(item => _replaceRootDirTags(rootDir, item));
  108. }
  109. if (config instanceof RegExp) {
  110. return config;
  111. }
  112. return _replaceRootDirInObject(rootDir, config);
  113. case 'string':
  114. return replaceRootDirInPath(rootDir, config);
  115. }
  116. return config;
  117. });
  118. /**
  119. * Finds the test environment to use:
  120. *
  121. * 1. looks for jest-environment-<name> relative to project.
  122. * 1. looks for jest-environment-<name> relative to Jest.
  123. * 1. looks for <name> relative to project.
  124. * 1. looks for <name> relative to Jest.
  125. */
  126. const getTestEnvironment = (exports.getTestEnvironment = config => {
  127. const env = replaceRootDirInPath(config.rootDir, config.testEnvironment);
  128. let module = (_jestResolve || _load_jestResolve()).default.findNodeModule(
  129. `jest-environment-${env}`,
  130. {
  131. basedir: config.rootDir
  132. }
  133. );
  134. if (module) {
  135. return module;
  136. }
  137. try {
  138. return require.resolve(`jest-environment-${env}`);
  139. } catch (e) {}
  140. module = (_jestResolve || _load_jestResolve()).default.findNodeModule(env, {
  141. basedir: config.rootDir
  142. });
  143. if (module) {
  144. return module;
  145. }
  146. try {
  147. return require.resolve(env);
  148. } catch (e) {}
  149. throw createValidationError(
  150. ` Test environment ${(_chalk || _load_chalk()).default.bold(
  151. env
  152. )} cannot be found. Make sure the ${(_chalk || _load_chalk()).default.bold(
  153. 'testEnvironment'
  154. )} configuration option points to an existing node module.`
  155. );
  156. });
  157. const isJSONString = (exports.isJSONString = text =>
  158. text &&
  159. typeof text === 'string' &&
  160. text.startsWith('{') &&
  161. text.endsWith('}'));