Skip to content

Commit 5017eba

Browse files
feat(config): add support for multiple paths in module name mapper
1 parent 0f93378 commit 5017eba

3 files changed

Lines changed: 13 additions & 18 deletions

File tree

src/config/paths-to-module-name-mapper.spec.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const tsconfigMap = {
99
'api/*': ['src/api/*'],
1010
'test/*': ['test/*'],
1111
'mocks/*': ['test/mocks/*'],
12-
'test/*/mock': ['test/mocks/*'],
12+
'test/*/mock': ['test/mocks/*', 'test/__mocks__/*'],
1313
}
1414

1515
describe('pathsToModuleNameMapper', () => {
@@ -21,7 +21,10 @@ Object {
2121
"^mocks/(.*)$": "test/mocks/$1",
2222
"^server$": "src/server",
2323
"^test/(.*)$": "test/$1",
24-
"^test/(.*)/mock$": "test/mocks/$1",
24+
"^test/(.*)/mock$": Array [
25+
"test/mocks/$1",
26+
"test/__mocks__/$1",
27+
],
2528
"^util/(.*)$": "src/util/$1",
2629
}
2730
`)
@@ -35,7 +38,10 @@ Object {
3538
"^mocks/(.*)$": "<rootDir>/test/mocks/$1",
3639
"^server$": "<rootDir>/src/server",
3740
"^test/(.*)$": "<rootDir>/test/$1",
38-
"^test/(.*)/mock$": "<rootDir>/test/mocks/$1",
41+
"^test/(.*)/mock$": Array [
42+
"<rootDir>/test/mocks/$1",
43+
"<rootDir>/test/__mocks__/$1",
44+
],
3945
"^util/(.*)$": "<rootDir>/src/util/$1",
4046
}
4147
`)
@@ -48,20 +54,16 @@ Object {
4854
pathsToModuleNameMapper({
4955
kept: ['src/kept'],
5056
'no-target': [],
51-
'too-many-target': ['one', 'two'],
5257
'too/*/many/*/stars': ['to/*/many/*/stars'],
5358
}),
5459
).toMatchInlineSnapshot(`
5560
Object {
5661
"^kept$": "src/kept",
57-
"^too\\\\-many\\\\-target$": "one",
5862
}
5963
`)
6064
expect(log.lines.warn).toMatchInlineSnapshot(`
6165
Array [
6266
"[level:40] Not mapping \\"no-target\\" because it has no target.
63-
",
64-
"[level:40] Mapping only to first target of \\"too-many-target\\" because it has more than one (2).
6567
",
6668
"[level:40] Not mapping \\"too/*/many/*/stars\\" because it has more than one star (\`*\`).
6769
",

src/config/paths-to-module-name-mapper.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,18 @@ export const pathsToModuleNameMapper = (
2626
if (toPaths.length === 0) {
2727
logger.warn(interpolate(Errors.NotMappingPathWithEmptyMap, { path: fromPath }))
2828
continue
29-
} else if (toPaths.length > 1) {
30-
logger.warn(
31-
interpolate(Errors.MappingOnlyFirstTargetOfPath, {
32-
path: fromPath,
33-
count: toPaths.length,
34-
}),
35-
)
3629
}
37-
const target = toPaths[0]
3830

3931
// split with '*'
4032
const segments = fromPath.split(/\*/g)
4133
if (segments.length === 1) {
34+
const paths = toPaths.map((target) => `${prefix}${target}`)
4235
pattern = `^${escapeRegex(fromPath)}$`
43-
jestMap[pattern] = `${prefix}${target}`
36+
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
4437
} else if (segments.length === 2) {
38+
const paths = toPaths.map((target) => `${prefix}${target.replace(/\*/g, '$1')}`)
4539
pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`
46-
jestMap[pattern] = `${prefix}${target.replace(/\*/g, '$1')}`
40+
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
4741
} else {
4842
logger.warn(interpolate(Errors.NotMappingMultiStarPath, { path: fromPath }))
4943
continue

src/util/messages.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const enum Errors {
1212
UnableToCompileTypeScript = '{{diagnostics}}',
1313
NotMappingMultiStarPath = 'Not mapping "{{path}}" because it has more than one star (`*`).',
1414
NotMappingPathWithEmptyMap = 'Not mapping "{{path}}" because it has no target.',
15-
MappingOnlyFirstTargetOfPath = 'Mapping only to first target of "{{path}}" because it has more than one ({{count}}).',
1615
GotJsFileButAllowJsFalse = 'Got a `.js` file to compile while `allowJs` option is not set to `true` (file: {{path}}). To fix this:\n - if you want TypeScript to process JS files, set `allowJs` to `true` in your TypeScript config (usually tsconfig.json)\n - if you do not want TypeScript to process your `.js` files, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match `.js` files anymore',
1716
GotUnknownFileTypeWithoutBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore.',
1817
GotUnknownFileTypeWithBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files.',

0 commit comments

Comments
 (0)