ts-jest cannot resolve tsconfig aliases
Asked Answered
E

2

6

I have a typescript project and I setup aliases in ts.config.json

{
  "compilerOptions": {    
    "paths": {   
      "@pkg/*": ["./packages/*"],
        },
  }
}

in my ts files I can shorten my import paths

// example.ts
import {someThing} from '@pkg/mypackage'

it works fine with tsc and vscode can recognize the alias path correctly, but when I run npm t which runs jest it fails

Cannot find module '@pkg/mypackage' from 'example.ts'

jest.config.js

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
};

I added this to my package.json file

"jest": {
    "moduleNameMapper": {
      "@pkg/(.*)": "<rootDir>/packages/$1"
    }
  }

I managed to use pathsToModuleNameMapper, but I had this issue https://github.com/kulshekhar/ts-jest/issues/2709

Eustis answered 29/6, 2021 at 20:59 Comment(0)
P
4

I had the same problem, but managed to get it working by using a couple of plugins. I also have some extra matchers at the end for some additional test types.

My Jest-base.config.js has the tsconfig-paths-jest plugin installed and running. This plugin solved my tsconfig path issues.

I use a common base file for common configuration between unit tests and end to end tests, both of which I run via Jest currently.

jest-base.config.ts

const tsconfig = require('./tsconfig.json');
const moduleNameMapper = require('tsconfig-paths-jest')(tsconfig);

module.exports = {
    moduleNameMapper,
    preset: 'ts-jest',
    testEnvironment: 'node',

    rootDir: './',

    collectCoverage: true,
    collectCoverageFrom: [
        '<rootDir>/**/*.ts',
        '!<rootDir>/**/*.interface.ts',
        '!<rootDir>/**/*.mock.ts',
        '!<rootDir>/**/*.module.ts',
        '!<rootDir>/**/__mock__/*',
        '!<rootDir>/src/main.ts'
    ],
    coverageProvider: 'v8',
    coverageReporters: [
        'clover',
        'json',
        'lcov',
        'text',
        'text-summary'
    ],
    resetModules: true,
    setupFiles: [
        'dotenv/config'
    ],
    // Add the community jest-extended matchers
    setupFilesAfterEnv: [
        'jest-extended'
    ],
    verbose: false
};

My jest.config.js (for unit tests) will extend my jest-base.config.js to add unit test specific code such as coverage requirements, where to store output for coverage etc.

jest.config.js

const JestBaseConfiguration = require('./jest-base.config');

module.exports = Object.assign(JestBaseConfiguration, {
moduleFileExtensions: ['js', 'json', 'ts'],
testRegex: '.e2e-spec.ts$',
transform: {
    '^.+\\.(t|j)s$': 'ts-jest'
},
...
Perspiration answered 30/6, 2021 at 14:56 Comment(5)
check this github.com/kulshekhar/ts-jest/issues/2709Eustis
Using the plugin, I do not have to duplicate the paths in my Jest config with the regex expression. It simply uses the ones defined in the tsconfig.json.Perspiration
I mentioned that I already use it, but with no luckEustis
@Sheldeeb I did not add it in package.json, via your method. I did do it via the javascript configuration as I posted, and it works for my processing, where I have a test that even uses @APP (./src) and backs up one level to package.json. The <rootDir> setting also has impact on the actual pathing.Perspiration
Try this kulshekhar.github.io/ts-jest/docs/getting-started/paths-mappingBovid
H
1

I solved this problem by adding an moduleNameMapper property to the jest.config.ts file.

ts.config.json:

"paths": {
        "@/*": ["src/*"],
        "@": ["src/index.ts"]
    },

jest.config.json:

import type { Config } from "jest";

const config: Config = {
    preset: "ts-jest",
    testEnvironment: "node",
    moduleNameMapper: {
        "@/(.*)": "<rootDir>/src/$1",
    },
};

export default config;
Haven answered 8/5 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.