utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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) { return obj && obj.__esModule ? obj : { default: obj }; }
  23. const BULLET = exports.BULLET = (_chalk || _load_chalk()).default.bold('\u25cf '); /**
  24. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  25. *
  26. * This source code is licensed under the MIT license found in the
  27. * LICENSE file in the root directory of this source tree.
  28. *
  29. *
  30. */
  31. const DOCUMENTATION_NOTE = exports.DOCUMENTATION_NOTE = ` ${(_chalk || _load_chalk()).default.bold('Configuration Documentation:')}
  32. https://facebook.github.io/jest/docs/configuration.html
  33. `;
  34. const createValidationError = message => {
  35. return new (_jestValidate || _load_jestValidate()).ValidationError(`${BULLET}Validation Error`, message, DOCUMENTATION_NOTE);
  36. };
  37. const resolve = exports.resolve = (rootDir, key, filePath) => {
  38. const module = (_jestResolve || _load_jestResolve()).default.findNodeModule(_replaceRootDirInPath(rootDir, filePath), {
  39. basedir: rootDir
  40. });
  41. if (!module) {
  42. throw createValidationError(` Module ${(_chalk || _load_chalk()).default.bold(filePath)} in the ${(_chalk || _load_chalk()).default.bold(key)} option was not found.`);
  43. }
  44. return module;
  45. };
  46. const escapeGlobCharacters = exports.escapeGlobCharacters = path => {
  47. return path.replace(/([()*{}\[\]!?\\])/g, '\\$1');
  48. };
  49. const _replaceRootDirInPath = exports._replaceRootDirInPath = (rootDir, filePath) => {
  50. if (!/^<rootDir>/.test(filePath)) {
  51. return filePath;
  52. }
  53. return (_path || _load_path()).default.resolve(rootDir, (_path || _load_path()).default.normalize('./' + filePath.substr('<rootDir>'.length)));
  54. };
  55. const _replaceRootDirInObject = (rootDir, config) => {
  56. if (config !== null) {
  57. const newConfig = {};
  58. for (const configKey in config) {
  59. newConfig[configKey] = configKey === 'rootDir' ? config[configKey] : _replaceRootDirTags(rootDir, config[configKey]);
  60. }
  61. return newConfig;
  62. }
  63. return config;
  64. };
  65. const _replaceRootDirTags = exports._replaceRootDirTags = (rootDir, config) => {
  66. switch (typeof config) {
  67. case 'object':
  68. if (Array.isArray(config)) {
  69. return config.map(item => _replaceRootDirTags(rootDir, item));
  70. }
  71. if (config instanceof RegExp) {
  72. return config;
  73. }
  74. return _replaceRootDirInObject(rootDir, config);
  75. case 'string':
  76. return _replaceRootDirInPath(rootDir, config);
  77. }
  78. return config;
  79. };
  80. /**
  81. * Finds the test environment to use:
  82. *
  83. * 1. looks for jest-environment-<name> relative to project.
  84. * 1. looks for jest-environment-<name> relative to Jest.
  85. * 1. looks for <name> relative to project.
  86. * 1. looks for <name> relative to Jest.
  87. */
  88. const getTestEnvironment = exports.getTestEnvironment = config => {
  89. const env = _replaceRootDirInPath(config.rootDir, config.testEnvironment);
  90. let module = (_jestResolve || _load_jestResolve()).default.findNodeModule(`jest-environment-${env}`, {
  91. basedir: config.rootDir
  92. });
  93. if (module) {
  94. return module;
  95. }
  96. try {
  97. return require.resolve(`jest-environment-${env}`);
  98. } catch (e) {}
  99. module = (_jestResolve || _load_jestResolve()).default.findNodeModule(env, { basedir: config.rootDir });
  100. if (module) {
  101. return module;
  102. }
  103. try {
  104. return require.resolve(env);
  105. } catch (e) {}
  106. throw createValidationError(` Test environment ${(_chalk || _load_chalk()).default.bold(env)} cannot be found. Make sure the ${(_chalk || _load_chalk()).default.bold('testEnvironment')} configuration option points to an existing node module.`);
  107. };
  108. const isJSONString = exports.isJSONString = text => text && typeof text === 'string' && text.startsWith('{') && text.endsWith('}');