No
No. I don't think this is relevant.
I couldn't find any docs on how to configure Jest
System:
OS: macOS 10.14
CPU: x64 Intel(R) Core(TM) 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
moduleNameMapper to the package.json jest entryI'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.

If you want to render the svg as a React component you should be using the following:
import { ReactComponent as Alchemy } from './icons/alchemy.svg'
// ... later
<Alchemy {...this.props} css={{ ... }} />
I'm not sure where you found your example using raw-loader, but that's not expected to work.
Your Jest configuration problem goes away when you do this.
Hi thanks for responding.I need to dynamically load the SVG files based on a given prop though. I can't figure out how to do that with the import alias syntax.
I'd still like to use aliases if possible without ejecting
Maybe the import all macro?
https://github.com/kentcdodds/import-all.macro
Unfortunately, dynamic imports don't currently work with the import as syntax: https://github.com/facebook/create-react-app/issues/5276
The import-all.macro doesn't load raw files unfortunately. I suppose I'll need to figure out how to precompile my icons in a build process or something, which is a bit awkward for development.
I've managed to get something working with https://github.com/smooth-code/svgr. Thanks everybody!
My original question still stands though. Why can we not extend moduleNameMapper for tests so that we can add aliases? Should I be using a jest plugin to do that instead?
@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!
Thanks @patricklafrance!