-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Idea: dontMock
has option to add recursively
#204
Comments
Yea this has come up a few times before. It's a bit of a tricky subject which mostly boils down to the fact that modules and their closure state are shared between other modules. So, while it may be advantageous to deep-mock one module, that may mean you are unintentionally mocking a deep-recursive dependency module that a 3rd module might expect not to be mocked (or maybe you've even explicitly The above situation, of course, assumes that mocking is a global concept -- as it is today in Jest. But we could also consider the idea that it's not, and that deep Ultimately it's a trade-off: Do we deal with a pretty annoying number of explicit I'd really love to find a good solution here, but between the two options we haven't thought of a good one yet. |
Yes, I can see that. I definitely think being explicit about exactly what one is doing with Feel free to close this ticket if you'd like; I'm going to ramble on a bit more below with some related ideas I've had, if you don't mind. Better Docs and Examples for MockingI've been testing with
EDIT: And…I should have worked more before posting. That mock for
If I'm not too far off base, I may be able to make a pull request for some of these documentation items. Let me know! Generation of mocksBeing new to Actually, as I think about it, perhaps the generated mock should just do a regular User-contributed mocksAgain, just brainstorming—but perhaps some sort of user-contributed mocks of various modules would make Thanks for listening! :-D |
I forgot to say…I've really enjoyed using |
A glob-styled syntax for |
+1 (I am brand new to Jest, so if that's way off the mark don't hesitate to say so). |
Perhaps this isn't the place to ask, but why would you want to mock libraries in |
@rattrayalex If it's something like For something like So you can't generally assume node_modules should / shouldn't be mocked by default. |
Thanks @jacobstr, that makes a lot of sense! Do you think it might be worthwhile to mention To be honest, though, I don't think Jest is right for me, as a total newcomer to testing. I don't really understand the utility of mocking because I haven't had to do it myself yet. If you think that's an accurate assessment (maybe not?) it may be worth mentioning in the docs. Or just saying more explicitly who Jest is for. I wouldn't say this is a big issue. |
Difference between dontMock and Mock in jest? |
I am seeing the polar opposite in 0.5.2. The requires of an unmocked module are not mocked at all. I'm using a manual mock, though... as in this: // __mocks__/NewProjectPage.js
global.$ = require.requireActual('jquery');
module.exports = require.requireActual('pages/NewProjectPage'); |
Jest 0.9 should work much better in all of these cases. node_modules are now by default deep-unmocked as well, as that was a frequent source of issues with npm3. We have also updated the documentation to be more clear and we are exploring more ways of improving (un)mocking. I think this task is resolved but I'm happy to reopen if there is anything left to do here for now. |
Is there a difference in behaviours between We observed that in a test that uses Adding cheerio to Symptomatically we saw the effect mentioned in this issue: which the original reporter mentions occurs with a Karma set up, so our problem may not actually relate to this issue. However, since it seems we were able to fix it with a specific configuration of mock disablements, which has led me to believe that in our case it could be a problem with Jest's mocking (or unmocking) behaviour. |
Hey! |
@goivemaster could you update this issue with our specifics? Otherwise I will edit this when I can pull the latest version later today. Thanks @cpojer |
Jest configuration :
If we put something inside unmockedModulePathPatterns should we expect all of its dependencies to also be unmocked? It appears that this is not the case. After adding enzyme to the unmockedModulePathPatterns we have been seeing errors relating to its dependencies remaining mocked hence the presence of cheerio and lodash. Is this a problem with Jest? |
They should be unmocked. Although I recommend specifying something like |
this is the latest version of jest on npm: 11.0.0 is the latest of 81 releases, we see no difference when adding the longer path. we notice enzyme has no node_modules directory of its own
would this be causing the problem or does jest just look at the package.json of each package specified under unmockedModulePathPatterns? |
@goivemaster did you have any luck on getting enzyme working? I hit exactly the same problem as you hit. When I use |
just a hunch, but this feels like it's related to Enzyme not having its own node_modules directory - this kind of transitive mocking seems to behave with other dependencies. For our use case (I work with @goivemaster) we were able to get away with unmocking Hoping @cpojer might have some insight on this one? |
That's weird, which version of Jest are you using? npm modules should definitely have transitive dependencies unmocked. If that's not the case, then that is a bug (the test for this is passing in Jest right now, though). |
@cpojer It seems to have something to do with running multiple tests in one run. When I run tests individually they work, but when I run all the test suites together it fails depending on the order in which the test run/finish I have different tests failing. Can it be that when test A requires module X to be mocked (and doesn't specify anything about module X, because mocking is the default) and a test B wants module X unmocked that test A might get an unmocked version. The opposite also seems true if you have automock-off and explicitly mock a module in a test you might get it mocked in another test that has automock-off. I should try to create a simple reproducible test case I/we can use to debug the problem. Because in my real project I have way to many tests to make it easily debuggable, but it is hard to create such a test case because I don't know the internals, and have only a hunch about what is causing the problem. |
Oh I see. Can you upgrade to Jest 12 and let me know if this problem still persists? We fixed something around this issue :) |
thanks, upgrading to Jest 12 now, will revert the workarounds of explicitly calling autmockoff, dontmock and mock, and then report back |
@cpojer Thanks! Upgrading from Jest 11 to 12 indeed solved the problem. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I was trying to call
dontMock
on thejoi
library (a schema validation library), and I had to add a lot ofdontMock
commands to get it to work—basically one for every filerequire
d in the library, recursively. While this makes sense in many situations, it would sure be nice to be able to tell Jest todontMock
a file and and files itrequire
s.One option could be something like
jest.dontMock( moduleName, [recursive] )
, whererecursive
is a boolean that defaults tofalse
, matching the current behavior. If it is set totrue
, it would applydontMock
recursively.Another option might be to base it off of what is being
require
d: it seems to me that callingdontMock
on a module innode_modules
would usually want a recursivedontMock
, whereas most other cases would want the non-recursive variety. Perhaps that API is too confusing.Thoughts?
The text was updated successfully, but these errors were encountered: