Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing with Jest and Webpack aliases #1909

Closed
FateZeros opened this issue Mar 30, 2017 · 5 comments
Closed

Testing with Jest and Webpack aliases #1909

FateZeros opened this issue Mar 30, 2017 · 5 comments

Comments

@FateZeros
Copy link

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 ?

@Timer
Copy link
Contributor

Timer commented Mar 30, 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)

@FateZeros
Copy link
Author

I want kr match ./src, and 'schoolBackend' match ./src/application/backend/school.
How can I set my jest config ?
when I run npm run test , I can find my webpack alias.

@Timer
Copy link
Contributor

Timer commented Mar 31, 2017

Webpack does not use regex like Jest, you must make the changes explained above.

@FateZeros
Copy link
Author

Can jest-webpack-alias, Jest/Webpack docs work in my problem ?
I read the Jest/Webpack docs, but it did not work.Am I in the wrong direction ?

@Timer
Copy link
Contributor

Timer commented 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 jestjs/jest#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.

@Timer Timer closed this as completed Mar 31, 2017
@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants