fix: Support relative tsconfig paths on multiple levels in jest config (no-changelog) (#14162)

This commit is contained in:
Alex Grozav
2025-03-25 17:32:24 +02:00
committed by GitHub
parent 53e11b19ad
commit f5f8abbbec

View File

@@ -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')
? '<rootDir>' + mapping + '/$1'
: '<rootDir>' + (baseUrl ? `/${baseUrl.replace(/^\.\//, '')}` : '') + mapping + '/$1';
return acc;
}, {}),
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: `<rootDir>${compilerOptions.baseUrl ? `/${compilerOptions.baseUrl.replace(/^\.\//, '')}` : ''}` }),
setupFilesAfterEnv: ['jest-expect-message'],
collectCoverage: isCoverageEnabled,
coverageReporters: ['text-summary', 'lcov', 'html-spa'],