import { config } from "dotenv"; import type { Config } from "@jest/types"; import fs from "fs/promises"; import { pathsToModuleNameMapper } from "ts-jest"; import path from "path"; // Or async function export default async (): Promise => { config({ path: path.resolve(process.cwd(), "test.env"), debug: true, override: true }); return { verbose: false, clearMocks: true, errorOnDeprecated: true, preset: "ts-jest", testEnvironment: "node", rootDir: "./", modulePathIgnorePatterns: ["/dist/"], cache: false, silent: false, moduleNameMapper: await getTSConfigPathsToJest(), }; }; async function getTSConfigPathsToJest() { const tsconfigWithComments = await fs.readFile("./tsconfig.json", { encoding: "utf8" }); // Remove comments const tsconfigText = tsconfigWithComments.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m, ); // Get the file with types const tsconfig = JSON.parse(tsconfigText); // Get all the paths from the compiler and convert them to something that jest understands const moduleNameMapper = pathsToModuleNameMapper(tsconfig.compilerOptions.paths, { prefix: "/src/" }); return moduleNameMapper; }