helpers.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.findSiblingsWithFileExtension = exports.decodePossibleOutsideJestVmPath = exports.createOutsideJestVmPath = void 0;
  6. function path() {
  7. const data = _interopRequireWildcard(require('path'));
  8. path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _glob() {
  14. const data = _interopRequireDefault(require('glob'));
  15. _glob = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _slash() {
  21. const data = _interopRequireDefault(require('slash'));
  22. _slash = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _interopRequireDefault(obj) {
  28. return obj && obj.__esModule ? obj : {default: obj};
  29. }
  30. function _getRequireWildcardCache() {
  31. if (typeof WeakMap !== 'function') return null;
  32. var cache = new WeakMap();
  33. _getRequireWildcardCache = function () {
  34. return cache;
  35. };
  36. return cache;
  37. }
  38. function _interopRequireWildcard(obj) {
  39. if (obj && obj.__esModule) {
  40. return obj;
  41. }
  42. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  43. return {default: obj};
  44. }
  45. var cache = _getRequireWildcardCache();
  46. if (cache && cache.has(obj)) {
  47. return cache.get(obj);
  48. }
  49. var newObj = {};
  50. var hasPropertyDescriptor =
  51. Object.defineProperty && Object.getOwnPropertyDescriptor;
  52. for (var key in obj) {
  53. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  54. var desc = hasPropertyDescriptor
  55. ? Object.getOwnPropertyDescriptor(obj, key)
  56. : null;
  57. if (desc && (desc.get || desc.set)) {
  58. Object.defineProperty(newObj, key, desc);
  59. } else {
  60. newObj[key] = obj[key];
  61. }
  62. }
  63. }
  64. newObj.default = obj;
  65. if (cache) {
  66. cache.set(obj, newObj);
  67. }
  68. return newObj;
  69. }
  70. /**
  71. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  72. *
  73. * This source code is licensed under the MIT license found in the
  74. * LICENSE file in the root directory of this source tree.
  75. */
  76. const OUTSIDE_JEST_VM_PROTOCOL = 'jest-main:'; // String manipulation is easier here, fileURLToPath is only in newer Nodes,
  77. // plus setting non-standard protocols on URL objects is difficult.
  78. const createOutsideJestVmPath = path =>
  79. OUTSIDE_JEST_VM_PROTOCOL + '//' + encodeURIComponent(path);
  80. exports.createOutsideJestVmPath = createOutsideJestVmPath;
  81. const decodePossibleOutsideJestVmPath = outsideJestVmPath => {
  82. if (outsideJestVmPath.startsWith(OUTSIDE_JEST_VM_PROTOCOL)) {
  83. return decodeURIComponent(
  84. outsideJestVmPath.replace(
  85. new RegExp('^' + OUTSIDE_JEST_VM_PROTOCOL + '//'),
  86. ''
  87. )
  88. );
  89. }
  90. return undefined;
  91. };
  92. exports.decodePossibleOutsideJestVmPath = decodePossibleOutsideJestVmPath;
  93. const findSiblingsWithFileExtension = (
  94. moduleFileExtensions,
  95. from,
  96. moduleName
  97. ) => {
  98. if (!path().isAbsolute(moduleName) && path().extname(moduleName) === '') {
  99. const dirname = path().dirname(from);
  100. const pathToModule = path().resolve(dirname, moduleName);
  101. try {
  102. const slashedDirname = (0, _slash().default)(dirname);
  103. const matches = _glob()
  104. .default.sync(`${pathToModule}.*`)
  105. .map(match => (0, _slash().default)(match))
  106. .map(match => {
  107. const relativePath = path().posix.relative(slashedDirname, match);
  108. return path().posix.dirname(match) === slashedDirname
  109. ? `./${relativePath}`
  110. : relativePath;
  111. })
  112. .map(match => `\t'${match}'`)
  113. .join('\n');
  114. if (matches) {
  115. const foundMessage = `\n\nHowever, Jest was able to find:\n${matches}`;
  116. const mappedModuleFileExtensions = moduleFileExtensions
  117. .map(ext => `'${ext}'`)
  118. .join(', ');
  119. return (
  120. foundMessage +
  121. "\n\nYou might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently " +
  122. `[${mappedModuleFileExtensions}].\n\nSee https://jestjs.io/docs/en/configuration#modulefileextensions-arraystring`
  123. );
  124. }
  125. } catch {}
  126. }
  127. return '';
  128. };
  129. exports.findSiblingsWithFileExtension = findSiblingsWithFileExtension;