moduleNameMapper doesn't work for me
See original GitHub issueI change the path configuration in my tsconfig, for better paths. But now i cannot execute my testcases. So i checked the following documentaries:
Is that an error or a misconfiguration? By Error i will do a small repo.
Snippet tsconfig.json
...
        "rootDirs": [
          "src/",
          "test/"
        ],
        "baseUrl": ".",
        "paths": {
          "log": ["src/util/log"],
          "server": ["src/server"],
          "util/*": ["src/util/*"],
          "api/*": ["src/api/*"],
          "middleware/*": ["src/middleware/*"],
          "service/*": ["src/service/*"],
          "types/*": ["src/types/*"],
          "test/*": ["test/*"],
          "mocks/*": ["test/mocks/*"],
          "src/*": ["src/*"]
        },
Snippet package.json
...
    "globals": {
      "ts-jest": {
        "tsConfigFile": "tsconfig.json"
      }
...
    "moduleDirectories": [
      ".",
      "node_modules"
    ],
    "moduleNameMapper": {
      "log": "<rootDir>/src/util/log",
      "server": "<rootDir>/src/server",
      "util/(.*)": "<rootDir>/src/util/$1",
      "api/(.*)": "<rootDir>/src/api/$1",
      "middleware/(.*)": "<rootDir>/src/middleware/$1",
      "service/(.*)": "<rootDir>/src/service/$1",
      "types/(.*)": "<rootDir>/src/types/$1",
      "test/(.*)": "<rootDir>/test/$1",
      "mocks/(.*)": "<rootDir>/test/mocks/$1",
      "src/(.*)": "<rootDir>/src/$1"
    }
...
ERROR
● Test suite failed to run
   Configuration error:
   Could not locate module ../util/params (mapped as C:\Users\XXX\backend\src\util\params)
   Please check:
   "moduleNameMapper": {
     "/util\/(.*)/": "C:\Users\XXX\backend\src\util\$1"
   },
   "resolver": undefined
Issue Analytics
- State:
 - Created 6 years ago
 - Comments:27 (2 by maintainers)
 
Top Results From Across the Web
`moduleNameMapper` settings in jest.config.js doesn't work ...
This Components/ is an alias expression by moduleNameMapper , and I think it doesn't work in only CircleCI. jest --showConfig option tells me...
Read more >Aliases in React, Jest and VSCode - Lucas Ennouchi - Medium
Simple configuration of aliases (with duplicates) for Webpack, Jest and VsCode. Compatible with many JS libraries like React (including CRA) ...
Read more >Jest: Unexpected token 'export' - Sendbird Community
... moduleNameMapper to my jest config but it doesn't work and I didn't find any topic/thread about that. Can you help me with...
Read more >Getting jest to respect transformIgnorePatterns inside .bs.js file
I have been trying to get my tests to run with jest, after I switched from CRA to ... But it didn't solve...
Read more >Using https cdn imports with Jest tests in JavaScript - Evil Tester
We can use the Jest moduleNameMapper to use npm installed packages for ... of triggering this that I tried didn't let me use...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I almost gave up on this but finally fixed it.
The key below was that
moduleDirectorieswas not required to get it to work butmodulePathsis. Also a piece of advice is to use<rootDir>instead of./.Changing the
moduleDirectorieskey in the jest configuration got this working (I tested withjest --no-cachefrom the root directory)Hope this helps