How to use the pathsToModuleNameMapper function from ts-jest

Find comprehensive JavaScript ts-jest.pathsToModuleNameMapper code examples handpicked from public code repositorys.

ts-jest.pathsToModuleNameMapper creates a mapping of module paths to module names that can be used by Jest.

13
14
15
16
17
18
19
20
21
22
23
24


const tsConfig = resolve(__dirname, 'tsconfig.json')
const { compilerOptions } = parseJSON(
  readFileSync(tsConfig, { encoding: 'utf8' })
)
const pathsToModuleNames = pathsToModuleNameMapper(compilerOptions.paths, {
  prefix: `${__dirname}/`
})


const moduleNameMapper = MAP_PATHS_TO_MODULES ? pathsToModuleNames : undefined
fork icon16
star icon79
watch icon7

+ 66 other calls in file

87
88
89
90
91
92
93
94
95
96
//   "json",
//   "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
modulePaths: ['<rootDir>'],

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
fork icon4
star icon34
watch icon17

+ 60 other calls in file

How does ts-jest.pathsToModuleNameMapper work?

ts-jest.pathsToModuleNameMapper is a function provided by the ts-jest library that converts a TypeScript path mapping object into a Jest-compatible moduleNameMapper object, which maps import statements to corresponding module paths.

This is useful for configuring Jest to resolve TypeScript aliases in import statements. The function takes an input object that represents the path mapping configuration, and returns a moduleNameMapper object that can be used in Jest configuration. The resulting moduleNameMapper object can be passed to Jest to configure module name mappings.

45
46
47
48
49
50
51
52
53
54
testEnvironment: 'node',
rootDir: ROOT_DIR,
restoreMocks: true,
reporters: ['default'],
modulePathIgnorePatterns: ['dist'],
moduleNameMapper: pathsToModuleNameMapper(tsconfig.compilerOptions.paths, {
  prefix: `${ROOT_DIR}/`,
}),
collectCoverage: false,
cacheDirectory: resolve(ROOT_DIR, `${CI ? '' : 'node_modules/'}.cache/jest`),
fork icon507
star icon0
watch icon74

+ 37 other calls in file

88
89
90
91
92
93
94
95
96
  // "json",
  // "node"
],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
fork icon0
star icon2
watch icon1

+ 18 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const { pathsToModuleNameMapper } = require("ts-jest/utils");

const paths = {
  "@app/*": ["src/*"],
  "@lib/*": ["lib/*"],
};

const moduleNameMapper = pathsToModuleNameMapper(paths, {
  prefix: " /",
});

console.log(moduleNameMapper);

In this example, the paths object defines two aliases: @app/* and @lib/*, both of which map to directories relative to the project root. pathsToModuleNameMapper is used to convert these aliases to a format that can be used by Jest. The resulting moduleNameMapper object will map module names like @app/foo and @lib/bar to the corresponding directories in the project.

83
84
85
86
87
88
89
90
91
//   "json",
//   "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>' }),

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
fork icon0
star icon0
watch icon1

+ 20 other calls in file

92
93
94
95
96
97
98
99
100
101
//   "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: 
// pathsToModuleNameMapper({
//   "@/*": ["./src/*"]
// }),
{
  // same as paths in tsconfig.json
fork icon0
star icon0
watch icon1

+ 10 other calls in file

82
83
84
85
86
87
88
89
90
91
//   "json",
//   "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
  prefix: '<rootDir>/src/',
}),

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
fork icon0
star icon0
watch icon1

76
77
78
79
80
81
82
83
84
//   "tsx",
//   "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' }),

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
fork icon0
star icon0
watch icon1

83
84
85
86
87
88
89
90
91
//   "json",
//   "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {prefix: '<rootDir>'}),

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
fork icon0
star icon0
watch icon1

93
94
95
96
97
98
99
100
101
//   "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
modulePaths: [compilerOptions.baseUrl], // <-- This will be set to 'baseUrl' value
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths , { prefix: '<rootDir>/' } ),

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
fork icon0
star icon0
watch icon1

+ 53 other calls in file