-
-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Allow Jest to preprocess non-root node_modules #607
Comments
This is not intentional, no. We include instead of excluding so that Can you please explain your reasoning for doing it like this? Are you trying to avoid relative paths by doing so? |
Avoiding relative paths is the immediate goal; the long-term goal is to allow apps to be structured more like monorepos. |
This is interesting. I’m not sure this is the approach we would take but we need to provide support for something like this. |
This will be a part of #741. |
It still would be so useful if Jest had the option to not ignore non-root node_modules instead of having to use a workaround (given projects that use local modules aren't likely to change structure just to use Jest). |
Oh, to clarify, I’m happy to review a PR that adds |
The trouble is, For example, even with the following config, tests insides
But that's an issue with Jest itself, not CRA. |
Please file it in Jest repo, and we'll discuss it there. |
I've changed my mind. Let's just make |
I'll take this! |
Is this being solved in Jest or CRA? |
I think these are loose threads here: #1081 (comment) |
To be perfectly clear: I do not know how to make this work without breaking Facebook's specific use cases. Even getting Jest to work as it is currently documented -- that is, with |
@modernserf It really comes down to the general concept of Jest wanting to "just work" by default. The default behavior is going to be to ignore I could submit a PR to Jest to completely change that assumption and get If we built another system on top of it, like say a If the Currently jest does support something we can use for
This works by setting up a whitelist for I think the underlying problem is that Jest's project-level assumptions are kicking this whole concept in the teeth, and because that assumption & jest's mantra are so well entrenched I don't see this changing any time soon. I imagine @cpojer will have issues with any PR that changes big assumptions about how jest views your project without large discussions and maybe even a major version bump. @gaearon CRA is has some pre-processing set up when running jest I think to allow jest to test ES6 import/export files. What are our limitations in the |
Do I understand correctly that jestjs/jest#2796 is where we’re stuck right now, and we need to get feedback on it? |
Basically the way I understand Jest is that they aim for safe defaults that should work mostly out of the box. Jest seems to assume that all To get
I didn't feel comfortable doing Option 1 because that is a huge breaking change that Jest authors would probably throw out in a heartbeat. And for Option 2 it feels like a recipe for disaster as we now have a negated black list whitelisting a black list. This is why I opted for the It feels dirty and I haven't gotten any feedback from the jest team on that PR. (It's also out of date, i need to rebase it on the new master) but without architectural decisions from the Jest team I think we're stuck. TL;DR - Yes, for the reasons above it looks like we're stuck. |
Well @gaearon we're back to square one on this one, understandably the jest team wasn't comfortable making the neccesary large changes to support this, additionally it seems they're not comfortable exposing This kind of puts a decent sized snag in local |
From my understanding Jest 19+ fixed this with testMatch. |
@Timer are you referring to jestjs/jest#3538 ? I'd have to test but in my initial investigations into this it was a matter of how jest's haste map builds up the list of test file candidates by actively ignoring It basically would actively ignore it because it resides inside a The |
Not completely opposed to it, we can make it work, but all the PRs were outdated and didn't do all the things that were necessary. We are a bit resource constrained with Jest atm but it should be possible to make this change properly, if somebody is thorough enough. |
@cpojer I think what's not clear is the direction the Jest team would like to take on this. As I outlined in my PR, it has to do with changing assumptions of the
That's incredibly ambiguous, what is "proper" in the Jest team's eyes? Obviously a less hacky solution that passing I recall another PR on this issue that did something similar to that, but was rejected because the changes broke internal Facebook dependencies that the author had no way of taking into account. I'm not saying that the You say you're resource constrained, we get that. That's why we're here, willing to submit PRs (I've counted at least 3 so far) and help move this forward. Hell I've already moved on in my own project and accepted that this probably wont see the light of day, at least not for a LOOOOONG time (it's already gone through 3-4 major jest versions and a couple CRA versions without any traction or discussion being completely ignored). But tell me this, why would we spend hours of time coming up with a solution, when there's a decent chance it's just going to be shot down, with no discussion, over changes to the project that aren't compatible to your unspoken goals, or your undisclosed dependencies. This is why I proposed the Is it the best? No You say you're open to making this change if someone is thorough enough, in what regards? In magically navigating all the unspoken issues the Jest team will have with changing the jest-haste-map's behavior regarding |
@Reanmachine hey! That's a bit strong. Please keep in mind that right at this moment, there is no official Jest team at Facebook (this is changing currently, yay!). It's just me spending all my free time coding and being burned out on it, a bit. Because I rewrote large parts of Jest recently, I went through the PR list to close older pull requests that haven't had activity in a while and had merge conflicts. The problem with For most people, this kind of slowdown is probably not acceptable and will make people want to not use Jest. This issue has also not moved much in the last three months, telling me that it doesn't seem like something that many people are running into. To sum up the current state: the solution with I'm wondering if the solution could be to use |
@cpojer Sorry if I came off strong, it's just really frustrating to try and help when the other end is often-times a black box. It's good to hear jest is getting a more serious commitment from FB in the future. I suppose where the big disconnect lies is between the testing layer & the indexing layer. Correct me if I'm wrong but the purpose of the The problem is that the assumptions that All of the config options in jest seem to be centered around safe/smart defaults. You look at I think the ideal way for the user would be that if Right now we can configure the whitelist that overrides this behavior with I think that the goal would be to allow a jest user to go "hey, I only have external modules in We could accomplish this by using something similar to What lies after this is making sure that This is all stuff I'm willing to tackle but I'm left with a dillema: How do I go about modifying I'd like to hear your comments on all of this @cpojer, I'm willing to make the OSS contribution but I'd rather not waste the time if undisclosed non-oss projects on FB will kibosh any attempt to improve this situation. |
If you'd like to change jest-haste-map to be extended this way and the default will not generate larger amounts of unnecessary files to be kept track of by Jest, I think we can make this work. I dislike exposing new configuration options for something like this but if that's the only way to go and there are many people that would benefit from this, I'm not opposed to shipping it. This isn't so much an FB concern as it is a concern with the memory usage (and disk cache) as well as performance of |
I’m pushing this back to 0.11 as we’re cutting 0.10 very soon, and there’s no solution yet. |
This is not going to be resolved soon, I guess. |
Ah.. never mind, I found an acceptable workaround: Add |
@trungdq88 the easiest workaround is to make this folder structure:
By leveraging the |
Given the desire to continue ignoring node_modules unless it is a node_modules folder contained directly beneath src, I have gotten this to work by modifying createJestConfig.js in react-scripts-js by adding these patterns:
The goal is to continue to filter out /node_modules/ unless it is contained within a part of the path that exactly matches /src/node_modules/ or \src\node_modules\ Now I am able to maintain absolute referenceable imports that are directly contained within the src/node_modules folder or are simply symlinked into the src/node_modules folder. Regex patterns use negative lookahead rather than negative lookback ((?<!([/\\]src))[/\\]node_modules[/\\]) because javascript does not support negative lookback. This seems like a straightforward solution that works for this specific project structure without requiring any changes to Jest or the need to expose any further configuration within CRA. Is there a particular scenario in which this approach will break. From my usage so far it appears to work well with CRA and with VSCode. |
@mlesk did you verify that this works? When I was working on this, it didn't matter what you put in |
@modernserf Ok, jumped the gun a bit after coming back to this a few times I got excited when it finally worked, There are two scenarios:
With the haste configuration jest picks up the files in src/node_modules. However, I suspect this is may not be an acceptable general solution since it causes Haste to spit out some duplicate file name warnings before the tests run. Once the warnings fly by the tests do all run. The funny thing is that Haste does not always produce the warnings, only occasionally. Having absolutely no insight into how haste works it is hard for me to speculate exactly what this haste configuration does. This was an exercise in trial and error not first principles. |
Closing in favor of #1333. |
Update: @modernserf is working on a PR to this issue. Please don't submit other PRs.
Currently, create-react-app allows you to structure your app like this:
So you can import 3rd party modules with
import "react"
or local modules withimport "@myapp/foo"
. Webpack will preprocess the files insrc/node_modules
, es6 features (including imports) are converted with babel. I imagine this behavior is intentional, because typical babel-loader configurations ignore allnode_modules
directories.However, although Webpack will preprocess these files through Babel, Jest will not; if your tests imports one of these local modules, Jest will choke on the un-transpiled
import
statements.I think all one needs to do to enable this is add
preprocessorIgnorePatterns: ["<rootDir>/node_modules"]
to the Jest config -- this will continue to not preprocess regular node_modules, but will preprocess internal node_modules, so it should have no impact on existing apps.Update: @modernserf is working on a PR to this issue. Please don't submit other PRs.
The text was updated successfully, but these errors were encountered: