55 * @see {@link https://github.com/kulshekhar/ts-jest/blob/dd3523cb7571714f06f1ea2ed1e3cf11970fbfce/src/config/paths-to-module-name-mapper.ts }
66 */
77
8- import type { Config } from '@jest/types'
9- import type { CompilerOptions } from 'typescript'
10-
11- type TsPathMapping = Exclude < CompilerOptions [ 'paths' ] , undefined >
12- type JestPathMapping = Config . InitialOptions [ 'moduleNameMapper' ]
8+ /**
9+ * We don't need to escape all chars, so commented out is the real one
10+ * const escapeRegex = (str: string) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
11+ *
12+ * @param {string } str
13+ * @returns {string }
14+ */
15+ const escapeRegex = str => str . replace ( / [ - \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' )
1316
14- // we don't need to escape all chars, so commented out is the real one
15- // const escapeRegex = (str: string) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
16- const escapeRegex = ( str : string ) => str . replace ( / [ - \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' )
17+ /**
18+ * @typedef {Exclude<import('typescript').CompilerOptions['paths'], undefined> } TsPathMapping
19+ * @typedef {import('@jest/types').Config.InitialOptions['moduleNameMapper'] } JestPathMapping
20+ */
1721
18- export const pathsToModuleNameMapper = (
19- mapping : TsPathMapping ,
20- { prefix = '' , useESM = false } : { prefix ?: string ; useESM ?: boolean } = { } ,
21- ) : JestPathMapping => {
22- const jestMap : JestPathMapping = { }
22+ /**
23+ * Converts TypeScript path mappings to Jest module name mappings
24+ *
25+ * @param {TsPathMapping } mapping - The TypeScript path mapping object
26+ * @param {{prefix?: string, useESM?: boolean} } options - Configuration options
27+ * @returns {JestPathMapping }
28+ */
29+ const pathsToModuleNameMapper = (
30+ mapping ,
31+ { prefix = '' , useESM = false } = { } ,
32+ ) => {
33+ /** @type {JestPathMapping } */
34+ const jestMap = { }
2335 for ( const fromPath of Object . keys ( mapping ) ) {
2436 const toPaths = mapping [ fromPath ]
2537 // check that we have only one target path
2638 if ( toPaths . length === 0 ) {
2739 console . warn ( `Not mapping "${ fromPath } " because it has no target.` )
28-
2940 continue
3041 }
3142
@@ -35,7 +46,6 @@ export const pathsToModuleNameMapper = (
3546 const paths = toPaths . map ( target => {
3647 const enrichedPrefix =
3748 prefix !== '' && ! prefix . endsWith ( '/' ) ? `${ prefix } /` : prefix
38-
3949 return `${ enrichedPrefix } ${ target } `
4050 } )
4151 const cjsPattern = `^${ escapeRegex ( fromPath ) } $`
@@ -48,7 +58,6 @@ export const pathsToModuleNameMapper = (
4858 : target
4959 const enrichedPrefix =
5060 prefix !== '' && ! prefix . endsWith ( '/' ) ? `${ prefix } /` : prefix
51-
5261 return `${ enrichedPrefix } ${ enrichedTarget . replace ( / \* / g, '$1' ) } `
5362 } )
5463 if ( useESM ) {
@@ -74,3 +83,5 @@ export const pathsToModuleNameMapper = (
7483
7584 return jestMap
7685}
86+
87+ module . exports = pathsToModuleNameMapper
0 commit comments