Testing with Jest and Webpack aliases
See original GitHub issuewhen I use create-react-app
my webpack aliases is
and my jest config is
import module like this
when I use npm run start
, I can run my project correctly.
But, when I use npm run test
, it throw an error I can not find the module which i import in the last picture .
How can I solve this ?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Testing with Jest and Webpack aliases - Stack Overflow
My question seems is moduleNameMapper just work to your source code, but doesn't work to your package code. How to resolve some packages...
Read more >Using with webpack - Jest
Jest can be used in projects that use webpack to manage assets, styles, ... This is pretty handy for React Snapshot Testing.
Read more >jest-webpack-alias - npm
Preprocessor for Jest that is able to resolve require() statements using webpack aliases.. Latest version: 3.3.4, last published: 3 years ...
Read more >Enhance Jest configuration with Module Aliases - Alex Jover
Adding module aliases is very simple and can keep your codebase much cleaner and easier to maintain. Jest makes it as well very...
Read more >Aliases in React, Jest and VSCode - Lucas Ennouchi - Medium
Simple configuration of aliases (with duplicates) for Webpack, Jest and VsCode. ... npm run test (with JEST) and the auto imports in VSCode....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I suspect it’s because you have a
$
(match end) in your JestmoduleNameMapper
config, as^kr$
does not matchkr/utils
.You probably need a second case like
^kr[/](.+)
which maps to<rootDir>/src/$1
.(your mileage may vary, this is an educated guess)
I’m going to close this since it isn’t very actionable by us. Basically, webpack knows a lot more about modules than Jest and knows how to remap all usages of
kr
for you. Jest relies on custom built regex by you, which requires you to match your slash-includes separately.You can see https://github.com/facebook/jest/pull/1723 for more details.
Please try the changes mentioned above. If that doesn’t work, you might find success using
jest-webpack-alias
, but we cannot provide you with support whilst using that. It seems to me like you’re relying on additional tooling unnecessarily – perhaps you ejected too early without looking at alternatives to solve your problems.