jest.config.js:
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');
pathsToModuleNameMapper(compilerOptions.paths, { prefix: compilerOptions.baseUrl } )
tsconfig.json:
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"components/*": ["components/*"]
}
},
}
pathsToModuleNameMapper compiles to:
{
'^components/(.*)$': 'srccomponents/$1'
}
but must be:
{
'^components/(.*)$': '.src/components/$1'
}
Solution:
you need to normalize the path first:
const path = require('path')
function pathsToModuleNameMapper (childPath, {prefix}){
// const totalPath = prefix + childPath; its WRONG
const totalPath = path.join(prefix, childPath); // its GOOD
}
jest.config.js:
tsconfig.json:
{ "compilerOptions": { "baseUrl": "src", "paths": { "components/*": ["components/*"] } }, }pathsToModuleNameMapper compiles to:
but must be:
Solution:
you need to normalize the path first: