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.

Module resolution fails when using both haste and moduleNameMapper config options

See original GitHub issue

🐛 Bug Report

Utilizing both haste and moduleNameMapper config options causes Jest’s module resolution to fail.

To Reproduce

  1. Clone the test case repository.
  2. yarn install
  3. yarn test

Expected behavior

The required module resolves and the test passes.

Link to repl or repo (highly encouraged)

https://github.com/HealthTeacher/jest-haste-module-name-mapper

Run npx envinfo --preset jest

Paste the results here:

System:
  OS: macOS 10.14
  CPU: x64 Intel(R) Core(TM) i7-4750HQ CPU @ 2.00GHz
Binaries:
  Node: 10.8.0 - ~/.asdf/shims/node
  Yarn: 1.10.1 - /usr/local/bin/yarn
  npm: 6.2.0 - ~/.asdf/shims/npm
npmPackages:
  jest: ^23.6.0 => 23.6.0

Notes

  • Replacing the root directory alias require with a relative require fixes the related test: const Greeter = require('./components/Greeter');
  • Relocating components/Greeter/index.web.js to components/Greeter/index.js also fixes the related test.
  • The combination of haste and moduleNameMapper config appears to cause an issue.

Error Output

FAIL  __tests__/index.test.js
 ● Test suite failed to run

   Configuration error:

   Could not locate module ~/components/Greeter mapped as:
   /Users/username/Sites/oss/jest-haste-module-name-mapper/components/Greeter.

   Please check your configuration for these entries:
   {
     "moduleNameMapper": {
       "/^~\/(.*)/": "/Users/username/Sites/oss/jest-haste-module-name-mapper/$1"
     },
     "resolver": null
   }

   > 1 | const Greeter = require('~/components/Greeter');
       |                         ^
     2 |
     3 | // Replacing the above `require` with below fixes the related test
     4 | // const Greeter = require('./components/Greeter');

     at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:411:17)
     at Object.<anonymous> (index.js:1:25)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rickhanloniicommented, Dec 1, 2018

Going to optimistically close, if it’s still an issue let us know and we’ll re-open 👍

0reactions
github-actions[bot]commented, May 12, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest --watch hangs silently if moduleNameMapper cannot ...
I'm having the same issue, with the moduleNameMapper jest --watch hangs. Removing the moduleNameMapper it works (well, runs the tests and fails ......
Read more >
Configuring Jest
This option allows the use of a custom global setup module, which must export a function (it can be sync or async). The...
Read more >
`moduleNameMapper` settings in jest.config.js doesn't work ...
tsconfig-paths-jest is not usable in Jest >23. For current Jest 26 I got it working via: ...
Read more >
jest-haste-map | Yarn - Package Manager
Jest will set process.env.NODE_ENV to 'test' if it's not set to something else. You can use that in your configuration to conditionally setup...
Read more >
Setting up a Monorepo with React Native You.I and Yarn - G2i
Our goal is to have two apps in the same repository, and allow these two apps to use the same modules inside a...
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