@@ -4,8 +4,6 @@ import type { CompilerOptions } from 'typescript'
44
55import { rootLogger } from '../utils/logger'
66import { Errors , interpolate } from '../utils/messages'
7- import { join } from 'path'
8- import { normalizeSlashes } from '../utils/normalize-slashes'
97
108type TsPathMapping = Exclude < CompilerOptions [ 'paths' ] , undefined >
119type JestPathMapping = Config . InitialOptions [ 'moduleNameMapper' ]
@@ -18,7 +16,7 @@ const logger = rootLogger.child({ [LogContexts.namespace]: 'path-mapper' })
1816
1917export 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