5
5
* @see {@link https://github.com/kulshekhar/ts-jest/blob/dd3523cb7571714f06f1ea2ed1e3cf11970fbfce/src/config/paths-to-module-name-mapper.ts }
6
6
*/
7
7
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, '\\$&' )
13
16
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
+ */
17
21
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 = { }
23
35
for ( const fromPath of Object . keys ( mapping ) ) {
24
36
const toPaths = mapping [ fromPath ]
25
37
// check that we have only one target path
26
38
if ( toPaths . length === 0 ) {
27
39
console . warn ( `Not mapping "${ fromPath } " because it has no target.` )
28
-
29
40
continue
30
41
}
31
42
@@ -35,7 +46,6 @@ export const pathsToModuleNameMapper = (
35
46
const paths = toPaths . map ( target => {
36
47
const enrichedPrefix =
37
48
prefix !== '' && ! prefix . endsWith ( '/' ) ? `${ prefix } /` : prefix
38
-
39
49
return `${ enrichedPrefix } ${ target } `
40
50
} )
41
51
const cjsPattern = `^${ escapeRegex ( fromPath ) } $`
@@ -48,7 +58,6 @@ export const pathsToModuleNameMapper = (
48
58
: target
49
59
const enrichedPrefix =
50
60
prefix !== '' && ! prefix . endsWith ( '/' ) ? `${ prefix } /` : prefix
51
-
52
61
return `${ enrichedPrefix } ${ enrichedTarget . replace ( / \* / g, '$1' ) } `
53
62
} )
54
63
if ( useESM ) {
@@ -74,3 +83,5 @@ export const pathsToModuleNameMapper = (
74
83
75
84
return jestMap
76
85
}
86
+
87
+ module . exports = pathsToModuleNameMapper
0 commit comments