UNPKG

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