helpers.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.findSiblingsWithFileExtension = void 0;
  6. function _path() {
  7. const data = _interopRequireDefault(require('path'));
  8. _path = function _path() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _slash() {
  14. const data = _interopRequireDefault(require('slash'));
  15. _slash = function _slash() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _glob() {
  21. const data = _interopRequireDefault(require('glob'));
  22. _glob = function _glob() {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _interopRequireDefault(obj) {
  28. return obj && obj.__esModule ? obj : {default: obj};
  29. }
  30. /**
  31. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  32. *
  33. * This source code is licensed under the MIT license found in the
  34. * LICENSE file in the root directory of this source tree.
  35. */
  36. const findSiblingsWithFileExtension = (
  37. moduleFileExtensions,
  38. from,
  39. moduleName
  40. ) => {
  41. if (
  42. !_path().default.isAbsolute(moduleName) &&
  43. _path().default.extname(moduleName) === ''
  44. ) {
  45. const dirname = _path().default.dirname(from);
  46. const pathToModule = _path().default.resolve(dirname, moduleName);
  47. try {
  48. const slashedDirname = (0, _slash().default)(dirname);
  49. const matches = _glob()
  50. .default.sync(`${pathToModule}.*`)
  51. .map(match => (0, _slash().default)(match))
  52. .map(match => {
  53. const relativePath = _path().default.posix.relative(
  54. slashedDirname,
  55. match
  56. );
  57. return _path().default.posix.dirname(match) === slashedDirname
  58. ? `./${relativePath}`
  59. : relativePath;
  60. })
  61. .map(match => `\t'${match}'`)
  62. .join('\n');
  63. if (matches) {
  64. const foundMessage = `\n\nHowever, Jest was able to find:\n${matches}`;
  65. const mappedModuleFileExtensions = moduleFileExtensions
  66. .map(ext => `'${ext}'`)
  67. .join(', ');
  68. return (
  69. foundMessage +
  70. "\n\nYou might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently " +
  71. `[${mappedModuleFileExtensions}].\n\nSee https://jestjs.io/docs/en/configuration#modulefileextensions-array-string`
  72. );
  73. }
  74. } catch (ignored) {}
  75. }
  76. return '';
  77. };
  78. exports.findSiblingsWithFileExtension = findSiblingsWithFileExtension;