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.

Why is the Jest moduleNameMapper configuration not supported?

See original GitHub issue

Is this a bug report?

No

Did you try recovering your dependencies?

No. I don’t think this is relevant.

Which terms did you search for in User Guide?

I couldn’t find any docs on how to configure Jest

Environment

System: OS: macOS 10.14 CPU: x64 Intel® Core™ i7-7920HQ CPU @ 3.10GHz Binaries: Node: 11.0.0 - ~/.asdf/shims/node Yarn: 1.10.1 - /usr/local/bin/yarn npm: 6.4.1 - ~/.asdf/shims/npm Browsers: Chrome: 70.0.3538.77 Safari: 12.0 npmPackages: @types/react: 16.4.18 => 16.4.18 @types/react-dom: 16.0.9 => 16.0.9 react: ^16.6.0 => 16.6.0 react-dom: ^16.6.0 => 16.6.0 react-scripts: 2.1.0 => 2.1.0 npmGlobalPackages: create-react-app: 2.1.0

Steps to Reproduce

  1. Add moduleNameMapper to the package.json jest entry
  2. Run the tests

Expected Behavior

I’m using raw-loader to dynamically load SVG Icon files. If I don’t override the module, I get an error:

    Cannot find module '!raw-loader!./icons/alchemy.svg' from 'Icon.tsx'

      39 |         {...this.props}
      40 |         dangerouslySetInnerHTML={{
    > 41 |           __html: require(`!raw-loader!./${icon}`),
         |                   ^
      42 |         }}
      43 |         css={{
      44 |           boxSizing: 'border-box',

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
      at Icon.require [as render] (src/alchemy/Icon/Icon.tsx:41:19)

Test Suites: 2 failed, 2 passed, 4 total
Tests:       3 failed, 9 passed, 12 total
Snapshots:   0 total
Time:        3.041s
Ran all test suites.
error Command failed with exit code 1.

I’ve migrated from create-react-app-typescript where I was able to configure moduleNameMapper (they added it themselves) to stub out SVG files in my tests and add other aliases.

Actual Behavior

package_json_ _cra-jest-module-name-mapper

Reproducible Demo

https://github.com/aaronmcadam/cra-jest-module-name-mapper

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
patricklafrancecommented, Nov 7, 2018

@aaronmcadam by using craco to customize your CRA setup you could extends moduleNameMapper.

Here’s a recipe to do it: https://github.com/sharegate/craco/blob/master/recipes/add-webpack-alias-to-jest/craco.config.js

I hope this helps!

1reaction
Timercommented, Oct 31, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

`moduleNameMapper` settings in jest.config.js doesn't work ...
jest --showConfig option tells me there is no difference between local and CI environment. Is there any fault in my settings?
Read more >
Configuring Jest
Configuring Jest. The Jest philosophy is to work great by default, but sometimes you just need more configuration power.
Read more >
Using with webpack · Jest
If moduleNameMapper cannot fulfill your requirements, you can use Jest's transform config option to specify how assets are transformed.
Read more >
Configure Jest for Testing JavaScript Applications
You'll need to handle Webpack loaders, dynamic imports, and custom module resolution which Node.js does not support. In this course we'll go over...
Read more >
Setup Jest with ModuleAppMapper - A Development Journey
Working with Nrwl using a monorepo setup with multiple libraries ... but Jest will not recognise the mapping in tsconfig.json. and will fail...
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