Skip to content

Commit 3d9035b

Browse files
authored
fix(utils): revert path.join and add check on prefix ends with / (#1989)
Closes #1982
1 parent dcd836d commit 3d9035b

File tree

6 files changed

+15
-25
lines changed

6 files changed

+15
-25
lines changed

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

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import * as utils from 'ts-jest/utils'
2-
import { compilerOptions } from './tsconfig.json';
32

43
test('utils', () => {
54
expect(Object.keys(utils)).toEqual(['mocked', 'createJestPreset', 'pathsToModuleNameMapper'])
65
expect(typeof utils.mocked).toBe('function')
76
expect(typeof utils.createJestPreset).toBe('function')
87
expect(typeof utils.pathsToModuleNameMapper).toBe('function')
98
})
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: 0 additions & 14 deletions
This file was deleted.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: <rootDir>/ 1`] = `
44
Object {
5+
"^@foo\\\\-bar/common$": "<rootDir>/../common/dist/library",
56
"^api/(.*)$": "<rootDir>/src/api/$1",
67
"^client$": Array [
78
"<rootDir>/src/client",
@@ -21,6 +22,7 @@ Object {
2122
2223
exports[`pathsToModuleNameMapper should convert tsconfig mapping with given prefix: foo 1`] = `
2324
Object {
25+
"^@foo\\\\-bar/common$": "foo/../common/dist/library",
2426
"^api/(.*)$": "foo/src/api/$1",
2527
"^client$": Array [
2628
"foo/src/client",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ const tsconfigMap = {
1111
'test/*': ['test/*'],
1212
'mocks/*': ['test/mocks/*'],
1313
'test/*/mock': ['test/mocks/*', 'test/__mocks__/*'],
14+
'@foo-bar/common': ['../common/dist/library'],
1415
}
1516

1617
describe('pathsToModuleNameMapper', () => {
1718
it('should convert tsconfig mapping with no given prefix', () => {
1819
expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(`
1920
Object {
21+
"^@foo\\\\-bar/common$": "../common/dist/library",
2022
"^api/(.*)$": "src/api/$1",
2123
"^client$": Array [
2224
"src/client",

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ 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'
97

108
type TsPathMapping = Exclude<CompilerOptions['paths'], undefined>
119
type JestPathMapping = Config.InitialOptions['moduleNameMapper']
@@ -18,7 +16,7 @@ const logger = rootLogger.child({ [LogContexts.namespace]: 'path-mapper' })
1816

1917
export const pathsToModuleNameMapper = (
2018
mapping: TsPathMapping,
21-
{ prefix = '' }: { prefix?: string } = {},
19+
{ prefix = '' }: { prefix: string } = Object.create(null),
2220
): JestPathMapping => {
2321
const jestMap: JestPathMapping = {}
2422
for (const fromPath of Object.keys(mapping)) {
@@ -34,11 +32,19 @@ export const pathsToModuleNameMapper = (
3432
// split with '*'
3533
const segments = fromPath.split(/\*/g)
3634
if (segments.length === 1) {
37-
const paths = toPaths.map((target) => normalizeSlashes(join(prefix, target)))
35+
const paths = toPaths.map((target) => {
36+
const enrichedPrefix = prefix !== '' && !prefix.endsWith('/') ? `${prefix}/` : prefix
37+
38+
return `${enrichedPrefix}${target}`
39+
})
3840
pattern = `^${escapeRegex(fromPath)}$`
3941
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
4042
} else if (segments.length === 2) {
41-
const paths = toPaths.map((target) => normalizeSlashes(join(prefix, target.replace(/\*/g, '$1'))))
43+
const paths = toPaths.map((target) => {
44+
const enrichedPrefix = prefix !== '' && !prefix.endsWith('/') ? `${prefix}/` : prefix
45+
46+
return `${enrichedPrefix}${target.replace(/\*/g, '$1')}`
47+
})
4248
pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`
4349
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
4450
} else {

0 commit comments

Comments
 (0)