Skip to content

Commit 81fce4c

Browse files
authored
fix(utils): pathsToModuleNameMapper resolve path mapping with path.join (#1969)
Closes #1968
1 parent 1452ce4 commit 81fce4c

File tree

6 files changed

+67
-23
lines changed

6 files changed

+67
-23
lines changed

e2e/__cases__/utils/src/foo/bar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import * as utils from 'ts-jest/utils'
2+
import { compilerOptions } from './tsconfig.json';
23

34
test('utils', () => {
45
expect(Object.keys(utils)).toEqual(['mocked', 'createJestPreset', 'pathsToModuleNameMapper'])
56
expect(typeof utils.mocked).toBe('function')
67
expect(typeof utils.createJestPreset).toBe('function')
78
expect(typeof utils.pathsToModuleNameMapper).toBe('function')
89
})
10+
11+
test('pathsToModuleNameMapper', () => {
12+
expect(utils.pathsToModuleNameMapper(compilerOptions.paths, { prefix: compilerOptions.baseUrl } )).toEqual({'^foo/(.*)$': 'src/foo/$1'})
13+
})

e2e/__cases__/utils/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "src",
4+
"target": "ES5",
5+
"module": "CommonJS",
6+
"resolveJsonModule": true,
7+
"paths": {
8+
"foo/*": ["foo/*"]
9+
}
10+
},
11+
"exclude": [
12+
"node_modules"
13+
]
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: <rootDir>/ 1`] = `
4+
Object {
5+
"^api/(.*)$": "<rootDir>/src/api/$1",
6+
"^client$": Array [
7+
"<rootDir>/src/client",
8+
"<rootDir>/src/client/index",
9+
],
10+
"^log$": "<rootDir>/src/utils/log",
11+
"^mocks/(.*)$": "<rootDir>/test/mocks/$1",
12+
"^server$": "<rootDir>/src/server",
13+
"^test/(.*)$": "<rootDir>/test/$1",
14+
"^test/(.*)/mock$": Array [
15+
"<rootDir>/test/mocks/$1",
16+
"<rootDir>/test/__mocks__/$1",
17+
],
18+
"^util/(.*)$": "<rootDir>/src/utils/$1",
19+
}
20+
`;
21+
22+
exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: foo 1`] = `
23+
Object {
24+
"^api/(.*)$": "foo/src/api/$1",
25+
"^client$": Array [
26+
"foo/src/client",
27+
"foo/src/client/index",
28+
],
29+
"^log$": "foo/src/utils/log",
30+
"^mocks/(.*)$": "foo/test/mocks/$1",
31+
"^server$": "foo/src/server",
32+
"^test/(.*)$": "foo/test/$1",
33+
"^test/(.*)/mock$": Array [
34+
"foo/test/mocks/$1",
35+
"foo/test/__mocks__/$1",
36+
],
37+
"^util/(.*)$": "foo/src/utils/$1",
38+
}
39+
`;

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const tsconfigMap = {
1414
}
1515

1616
describe('pathsToModuleNameMapper', () => {
17-
it('should convert tsconfig mapping', () => {
17+
it('should convert tsconfig mapping with no given prefix', () => {
1818
expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(`
1919
Object {
2020
"^api/(.*)$": "src/api/$1",
@@ -35,25 +35,8 @@ describe('pathsToModuleNameMapper', () => {
3535
`)
3636
})
3737

38-
it('should use the given prefix', () => {
39-
expect(pathsToModuleNameMapper(tsconfigMap, { prefix: '<rootDir>/' })).toMatchInlineSnapshot(`
40-
Object {
41-
"^api/(.*)$": "<rootDir>/src/api/$1",
42-
"^client$": Array [
43-
"<rootDir>/src/client",
44-
"<rootDir>/src/client/index",
45-
],
46-
"^log$": "<rootDir>/src/utils/log",
47-
"^mocks/(.*)$": "<rootDir>/test/mocks/$1",
48-
"^server$": "<rootDir>/src/server",
49-
"^test/(.*)$": "<rootDir>/test/$1",
50-
"^test/(.*)/mock$": Array [
51-
"<rootDir>/test/mocks/$1",
52-
"<rootDir>/test/__mocks__/$1",
53-
],
54-
"^util/(.*)$": "<rootDir>/src/utils/$1",
55-
}
56-
`)
38+
it.each(['<rootDir>/', 'foo'])('should convert tsconfig mapping with given prefix', (prefix) => {
39+
expect(pathsToModuleNameMapper(tsconfigMap, { prefix })).toMatchSnapshot(prefix)
5740
})
5841

5942
it('should warn about mapping it cannot handle', () => {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { CompilerOptions } from 'typescript'
44

55
import { rootLogger } from '../utils/logger'
66
import { Errors, interpolate } from '../utils/messages'
7+
import { join } from 'path'
8+
import { normalizeSlashes } from '../utils/normalize-slashes'
79

810
type TsPathMapping = Exclude<CompilerOptions['paths'], undefined>
911
type JestPathMapping = Config.InitialOptions['moduleNameMapper']
@@ -25,22 +27,22 @@ export const pathsToModuleNameMapper = (
2527
// check that we have only one target path
2628
if (toPaths.length === 0) {
2729
logger.warn(interpolate(Errors.NotMappingPathWithEmptyMap, { path: fromPath }))
30+
2831
continue
2932
}
3033

3134
// split with '*'
3235
const segments = fromPath.split(/\*/g)
3336
if (segments.length === 1) {
34-
const paths = toPaths.map((target) => `${prefix}${target}`)
37+
const paths = toPaths.map((target) => normalizeSlashes(join(prefix, target)))
3538
pattern = `^${escapeRegex(fromPath)}$`
3639
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
3740
} else if (segments.length === 2) {
38-
const paths = toPaths.map((target) => `${prefix}${target.replace(/\*/g, '$1')}`)
41+
const paths = toPaths.map((target) => normalizeSlashes(join(prefix, target.replace(/\*/g, '$1'))))
3942
pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`
4043
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
4144
} else {
4245
logger.warn(interpolate(Errors.NotMappingMultiStarPath, { path: fromPath }))
43-
continue
4446
}
4547
}
4648

0 commit comments

Comments
 (0)