helpers.js 4.2 KB

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