-
-
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
feature: allow disabling of require-mock feature #10540
Comments
It's not just mocking, it's also what gives transformation of source code (for both future/custom syntax and code coverage) and code execution within a sandbox (e.g. allowing you to use To speed up tests you might wanna consider reducing the dependency tree (meaning less files to evaluate). But disabling such a core feature of Jest is not really something we wanna do. As a last resort you can plug in your own runtime that does whatever you want. I don't think it's documented, but it's |
@rix0rrr did you manage to alleviate the overhead? |
@SimenB I'm curious why |
Oh that's a very fair point. I didn't even think that far ahead, but you are correct. |
@rthreei yes, that's how requiring modules work - it's depth first following each |
@SimenB I think the suggestion is that there may be no need to use your own hooked version of The chances we'd be interested in on-the-fly-compilation, mocking, and code coverage of those modules is small.
There might be (potentially substantial) performance gains to be had by deferring to built-in Yes, mocking the direct dependency (thereby preventing the transitive tree from being loaded) would technically work, but it seems like a hard-to-use and blunt tool to achieve something that could be transparent. Plus, it probably reduces the value of your tests as you are now only testing up to the library boundary, and no longer testing the integration of your code and theirs. (Of course, of those features, mocking is the most contentious one. I can imagine someone to have mocked a transitive dependency—even though they probably shouldn't have. For backwards compatibility it should therefore be an option) |
That's still ignoring the sandboxing I brought up in my previous comment. We need to load all code per sandbox so they can run in their own environment (DOM or Node) and so that anything that messes with globals are contained and predictable. And we cannot execute some code within one realm and some in another. Your suggestion will make it incredibly hard to reason about what can do e.g. Jest will never add an option to purposefully break the sandbox and the guarantees that brings - that's the basis of Jest's value proposition. But if you wanna ignore those guarantees because it works for your specific use case I again recommend providing your own |
I guess I don't understand how sandboxing works well enough. 😅 Thanks for being patient! Got the message loud and clear. |
That's perfectly fine, I'm sorry if my message came across as confrontational. I know not many people have poked at the |
Thanks @SimenB !
I couldn't find documentation on |
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. |
🚀 Feature Proposal
A flag to allow the disabling of the (recursive) replacement of a module's
require
function, to improve testing speed for projects that don't need it.Motivation
We have a sizeable JavaScript project with many files. So many files in fact that the replacement of the
require
function with arequireModuleOrMock
function is starting to make a large difference in the runtime of our tests.A small subset of our tests, when run with native jest 26.4.2
When putting in the following (hacky) patch to disable the require replacement:
Our runtime looks like this:
In our project, the runtime overhead of this feature seems to be about 150%. We have hundreds of test suites like this one, each of them paying this penalty.
I'd gladly trade the ability to be able to mock the importing of a module--and rewrite the handful of tests that use it--for a 60% reduction in test times.
Example
Add a new flag
moduleMocking
(default:true
):Pitch
The Jest core platform implements this functionality without offering a way to disable it. I don't want it yet I'm forced to pay for it in execution time.
The text was updated successfully, but these errors were encountered: