Skip to content

Commit fe45ad7

Browse files
feat(config): add support for multiple paths in module name mapper
1 parent 6f059b4 commit fe45ad7

3 files changed

Lines changed: 22 additions & 18 deletions

File tree

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,31 @@ import { pathsToModuleNameMapper } from './paths-to-module-name-mapper'
55
const tsconfigMap = {
66
log: ['src/util/log'],
77
server: ['src/server'],
8+
client: ['src/client', 'src/client/index'],
89
'util/*': ['src/util/*'],
910
'api/*': ['src/api/*'],
1011
'test/*': ['test/*'],
1112
'mocks/*': ['test/mocks/*'],
12-
'test/*/mock': ['test/mocks/*'],
13+
'test/*/mock': ['test/mocks/*', 'test/__mocks__/*'],
1314
}
1415

1516
describe('pathsToModuleNameMapper', () => {
1617
it('should convert tsconfig mapping', () => {
1718
expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(`
1819
Object {
1920
"^api/(.*)$": "src/api/$1",
21+
"^client$": Array [
22+
"src/client",
23+
"src/client/index",
24+
],
2025
"^log$": "src/util/log",
2126
"^mocks/(.*)$": "test/mocks/$1",
2227
"^server$": "src/server",
2328
"^test/(.*)$": "test/$1",
24-
"^test/(.*)/mock$": "test/mocks/$1",
29+
"^test/(.*)/mock$": Array [
30+
"test/mocks/$1",
31+
"test/__mocks__/$1",
32+
],
2533
"^util/(.*)$": "src/util/$1",
2634
}
2735
`)
@@ -31,11 +39,18 @@ Object {
3139
expect(pathsToModuleNameMapper(tsconfigMap, { prefix: '<rootDir>/' })).toMatchInlineSnapshot(`
3240
Object {
3341
"^api/(.*)$": "<rootDir>/src/api/$1",
42+
"^client$": Array [
43+
"<rootDir>/src/client",
44+
"<rootDir>/src/client/index",
45+
],
3446
"^log$": "<rootDir>/src/util/log",
3547
"^mocks/(.*)$": "<rootDir>/test/mocks/$1",
3648
"^server$": "<rootDir>/src/server",
3749
"^test/(.*)$": "<rootDir>/test/$1",
38-
"^test/(.*)/mock$": "<rootDir>/test/mocks/$1",
50+
"^test/(.*)/mock$": Array [
51+
"<rootDir>/test/mocks/$1",
52+
"<rootDir>/test/__mocks__/$1",
53+
],
3954
"^util/(.*)$": "<rootDir>/src/util/$1",
4055
}
4156
`)
@@ -48,20 +63,16 @@ Object {
4863
pathsToModuleNameMapper({
4964
kept: ['src/kept'],
5065
'no-target': [],
51-
'too-many-target': ['one', 'two'],
5266
'too/*/many/*/stars': ['to/*/many/*/stars'],
5367
}),
5468
).toMatchInlineSnapshot(`
5569
Object {
5670
"^kept$": "src/kept",
57-
"^too\\\\-many\\\\-target$": "one",
5871
}
5972
`)
6073
expect(log.lines.warn).toMatchInlineSnapshot(`
6174
Array [
6275
"[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).
6576
",
6677
"[level:40] Not mapping \\"too/*/many/*/stars\\" because it has more than one star (\`*\`).
6778
",

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)