/
helpers.ts
50 lines (41 loc) · 1.54 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import path from 'path';
import slash from 'slash';
import glob from 'glob';
import {Config} from '@jest/types';
export const findSiblingsWithFileExtension = (
moduleFileExtensions: Config.ProjectConfig['moduleFileExtensions'],
from: Config.Path,
moduleName: string,
): string => {
if (!path.isAbsolute(moduleName) && path.extname(moduleName) === '') {
const dirname = path.dirname(from);
const pathToModule = path.resolve(dirname, moduleName);
try {
const slashedDirname = slash(dirname);
const matches = glob
.sync(`${pathToModule}.*`)
.map(match => slash(match))
.map(match => {
const relativePath = path.posix.relative(slashedDirname, match);
return path.posix.dirname(match) === slashedDirname
? `./${relativePath}`
: relativePath;
})
.map(match => `\t'${match}'`)
.join('\n');
if (matches) {
const foundMessage = `\n\nHowever, Jest was able to find:\n${matches}`;
const mappedModuleFileExtensions = moduleFileExtensions
.map(ext => `'${ext}'`)
.join(', ');
return (
foundMessage +
"\n\nYou might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently " +
`[${mappedModuleFileExtensions}].\n\nSee https://jestjs.io/docs/en/configuration#modulefileextensions-array-string`
);
}
} catch (ignored) {}
}
return '';
};