diff --git a/jest.config.js b/jest.config.js index 5c14b7800a..e089238141 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,4 +1,5 @@ -const { compilerOptions } = require('./tsconfig.json'); +const { pathsToModuleNameMapper } = require('ts-jest') +const { compilerOptions } = require('get-tsconfig').getTsconfig().config; /** @type {import('ts-jest').TsJestGlobalOptions} */ const tsJestOptions = { @@ -10,7 +11,6 @@ const tsJestOptions = { }, }; -const { baseUrl, paths } = require('get-tsconfig').getTsconfig().config?.compilerOptions; const isCoverageEnabled = process.env.COVERAGE_ENABLED === 'true'; @@ -24,15 +24,7 @@ const config = { '^.+\\.ts$': ['ts-jest', tsJestOptions], }, // This resolve the path mappings from the tsconfig relative to each jest.config.js - moduleNameMapper: Object.entries(paths || {}).reduce((acc, [path, [mapping]]) => { - path = `^${path.replace(/\/\*$/, '/(.*)$')}`; - mapping = mapping.replace(/^\.?\.\/(?:(.*)\/)?\*$/, '$1'); - mapping = mapping ? `/${mapping}` : ''; - acc[path] = mapping.startsWith('/test') - ? '' + mapping + '/$1' - : '' + (baseUrl ? `/${baseUrl.replace(/^\.\//, '')}` : '') + mapping + '/$1'; - return acc; - }, {}), + moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: `${compilerOptions.baseUrl ? `/${compilerOptions.baseUrl.replace(/^\.\//, '')}` : ''}` }), setupFilesAfterEnv: ['jest-expect-message'], collectCoverage: isCoverageEnabled, coverageReporters: ['text-summary', 'lcov', 'html-spa'],