Why is the Jest moduleNameMapper configuration not supported?
See original GitHub issueIs 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
- Add
moduleNameMapper
to thepackage.json
jest
entry - 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
Reproducible Demo
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
@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!
Maybe the import all macro? https://github.com/kentcdodds/import-all.macro