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.

Testing with Jest and Webpack aliases

See original GitHub issue

when I use create-react-app my webpack aliases is

image

and my jest config is qq20170330-230943 2x

import module like this image

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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
Timercommented, Mar 31, 2017

I suspect it’s because you have a $ (match end) in your Jest moduleNameMapper config, as ^kr$ does not match kr/utils.

You probably need a second case like ^kr[/](.+) which maps to <rootDir>/src/$1.

(your mileage may vary, this is an educated guess)

0reactions
Timercommented, Mar 31, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

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