typescriptjestjstsconfigts-jesttsconfig-paths

TS-Jest not resolving tsconfig paths


I have already added these paths to tsconfig.json:

{
    "compilerOptions": {
        "lib": ["ESNext"],
        "moduleResolution": "node",
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "removeComments": true,
        "sourceMap": true,
        "target": "ES2020",
        "outDir": "lib",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "baseUrl": ".",
        "paths": {
            "@lambdas/*": ["src/lambdas/*"],
            "@services/*": ["src/services/*"],
            "@types/*": ["src/@types/*"],
            "@configs/*": ["src/configs/*"],
            "@database/*": ["src/database/*"],
            "@entities/*": ["src/entities/*"],
            "@validations/*": ["src/validations/*"],
            "#serverless/*": ["serverless/*"]
        }
    },
    "include": ["src/**/*.ts", "serverless.ts"],
    "exclude": [
        "node_modules/**/*",
        ".serverless/**/*",
        ".webpack/**/*",
        "_warmup/**/*",
        ".vscode/**/*"
    ]
}

And in the jest.config.js I added this:

const { pathsToModuleNameMapper } = require('ts-jest/utils');
// eslint-disable-next-line import/extensions
const { compilerOptions } = require('./tsconfig.json');

// module.exports = { ...
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
// ...

But I still have the same behaviour: the intellisense doesn't even load the possible custom paths like those listed above as you can see in the image below: test file

That happens with this folder structure:

folder struct


Solution

  • You can create a new tsconfig file just for building. ex: copy your current tsconfig.json to tsconfig.build.json

    Remember to update esbuild options in serverless.ts

    ...
    custom: {
        esbuild: {
            tsconfig: 'tsconfig.build.ts'
        }
    }
    ...
    

    then create a new tsconfig.json for IDE(vscode) and test:

    {
      "extends": "./tsconfig.paths.json", // extend setting from build config
     
      "include": [
        "src/**/*.ts",
        "tests/**/*.ts" // include tests directory
      ],
      "exclude": [
        "node_modules/**/*",
        ".serverless/**/*",
        ".webpack/**/*",
        "_warmup/**/*",
        ".vscode/**/*"
      ]
    }