To enable absolute path import in our test files, we need to configure it first. ts-jest provides a helper to transform the mapping from tsconfig.
// jest.config.js
const { pathsToModuleNameMapper } = require("ts-jest");
const { compilerOptions } = require("./tsconfig");
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
// [...]
modulePaths: [compilerOptions.baseUrl],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: "<rootDir>/src", // this might depend on your tsconfig configuration
}),
};
References / Further Guide: https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping