Skip to content

moduleNameMapper doesn't handle multiple paths #1072

@jakeleboeuf

Description

@jakeleboeuf

Issue :

I'm having trouble configuring jest to resolve modules when using multiple paths in my tsconfig. It seems ts-jest will only resolve a single path- is this the case?

Expected behavior :

I would expect to find some solution to resolve paths when my tsconfig.json paths array has more than one path listed. After all, it is an array ¯_(ツ)_/¯

File structure is as follows
.
├── tsconfig.json
├── jest.config.js
├── src-common
|   └── components
|       └── Button.tsx
├── src-admin
|   └── components
|       └── Login.tsx
└── src-main
    └── components
        └── Share.tsx
relevant parts of tsconfig.json
{
  "baseUrl": ".",
  "paths": {
    "@components/*": ["src-common/components/ui/*", "src-admin/components/ui/*", "src-main/components/*],
  }
}
jest.config.js
module.exports = {
  setupFilesAfterEnv: ["react-testing-library/cleanup-after-each"],
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
  moduleNameMapper: {
    // Resolves a single path as expected 👍 
    "^@components/(.*)$": "<rootDir>/src-common/components/$1",

    // No regex in path 😢 
    "^@components/(.*)$": "<rootDir>/src-{common,admin,main}/components/$1",

    // No array in path 😢
    "^@components/(.*)$": [
      "<rootDir>/src-common/components/$1"
      "<rootDir>/src-admin/components/$1"
      "<rootDir>/src-main/components/$1"
    ],
  },
};

Debug log:

log file content
# content of ts-jest.log :
# Project is more complex than this example, and the output doesn't exactly match ☝️ 

Minimal repo :

Unfortunately Codesandbox doesn't support tsconfig.json paths :(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions