question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

moduleNameMapper doesn't work for me

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:27 (2 by maintainers)

github_iconTop GitHub Comments

84reactions
DarkLite1commented, Jun 22, 2020

I almost gave up on this but finally fixed it.

// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@environment": ["src/environment"],
      "@test-utils/*": ["src/test-utils/*"]
    },
}

The key below was that moduleDirectories was not required to get it to work but modulePaths is. Also a piece of advice is to use <rootDir> instead of ./.

// jest.config.js
const { pathsToModuleNameMapper } = require('ts-jest/utils')
const { compilerOptions } = require('./tsconfig')

module.exports = {
  roots: ['<rootDir>'],
  modulePaths: ['<rootDir>'],
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
}
56reactions
kulshekharcommented, Nov 8, 2017

Changing the moduleDirectories key in the jest configuration got this working (I tested with jest --no-cache from the root directory)

    "moduleDirectories": [
      ".",
      "src",
      "src/util",
      "node_modules"
    ]

Hope this helps

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found