Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Problems with resolving aliases #1290
I'm trying to set Jest to work with my webpack aliases and to achieve this I'm using the
I'm using the most recent versions of both Node and Jest. In my case, if I open this project in the console and run
Now I go to
So now a different solution:
Passes again. Let's remove the extension then, since it should work anyway:
Of course, I'm not sure exactly if this really is a problem with aliases, but it does work when put this way:
No, Jest has a lot more to offer than automocking. Automocking is useful in certain situations, like when you have a codebase that isn't tested at all with a huge amount of tightly connected dependencies. We have found at Facebook that after years of it being beneficial, it is not providing as much value as it once did and people choose to disable it more often.
Jest itself has a ton of other features that are critical to Facebook. I refer to the website for a subset of those features.
Regarding this individual bug, it doesn't look like I'm gonna get to this any time soon as I'm focused on Jest's performance and some other developer experience work for the next few months. If you'd like to help out to fix this, it is likely going to be somewhere around here:
This seems useful when you're using this functionality to provide mocks for assets like CSS files, but is kind of at odds with the idea of using webpack-like aliases, since the behavior changes depending on whether an alias is used or not. For instance:
This passes, since Jest actually imports the file required. But this:
...shows an error:
Could this be the root (or at least part) of the problem here? Calling
Perhaps it could be useful to provide a way of specifying whether a particular key/regexp should be automatically unmocked or not. Otherwise, you're forced to let go of automocking, plus if you ever mix relative paths and aliases, you get two differing behaviors. I think there should also be a note in the documentation that
In the meantime, a script preprocessor seems like still the best way to get aliases.
Jul 14, 2016
referenced this issue
Jul 14, 2016
To boil this down, it's not possible to use auto-mocking for any modules you've put in
This is perhaps most problematic when you have a webpack alias (as we do) for your root src directory that is then used extensively throughout the codebase. For example
If you put
Though in this case you'd have to make sure there was some kind of resolvable module in mocks for every module.
It would be nice if there were an option just to use webpack resolution to locate modules without affecting mock choices. Until then, we're going the route of manual mocking.
If you were interested, an example of such a preprocessor is a package called jest-webpack-alias. It didn't work for me for some reason I don't remember so I also wrote my own version, jest-alias-preprocessor. And lastly,