-
-
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
Mock Single Export #9456
Comments
Thanks for the detailed request!
Not too much of a biggie in my mind, but we could possible inject some bound helpers as the argument to the factory function, e.g. jest.mock('path/to/module', ({requireActual}) => ({
...requireActual();
x: jest.fn(() => 321),
})); Not sure if it's much of an improvement...
You shouldn't have to if you use
You can use
Fair enough, not sure we can do much about that - seems intrinsic to module mocking in general. In general one should avoid advanced dependency trees (and especially cycles), but that's way easier said than done 🙂
The "rub" you describe in the The general workaround to the "linking first" problem is probably gonna be to use dynamic imports ( Node has a See #9430 for some more thoughts on what native ESM means for Jest.
I agree with the pitch in principle, but I'm sceptical of adding APIs we know will be hard or impossible to replicate faithfully in an ESM environment. And the proposed API suffers from the same issue |
In terms of implementation, we could try transforming the modules to essentially expose internal setter functions -- e.g. if there's a That said, and I think I know the answer here, would that approach take it into transformer / Babel territory, or would this still fall into Jest's domain? |
Thanks for the detailed writeup @fongandrew and your response @SimenB, I agree with everything in there 😄
I would say it probably falls into both - Jest already applies |
Yeah, @rickhanlonii suggested rewriting Not directly related to the feature request here, but using babel to implement features is certainly a possibility |
I'm a pretty novice-level user of Jest, but I would find this extremely beneficial. @fongandrew nailed it on the head, a feature like his pitch would be super helpful when you're looking to mock just a single export of a module. @SimenB - I'd expect it to look as you suggested:
The current process isn't bad at all, but:
I'd think it would be more efficient to just overwrite the desired mock properties, but I'm not going to claim I know anything about the internals of Jest 😄 |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
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
Helper to mock a single export from an ES module. Something like
jest.spyOnModule('path/to/module', 'nameOfExport')
.Motivation
Suppose we have a module with multiple exports but we want to replace only one of them:
At the moment, if you want to replace just
x
in ☝️, you might do something like like this:This isn't ideal for a number of reasons:
__esModule: true
or some such to bridge the gap between Jest mocking things as CommonJS vs ESM.jest.mock
a method on the module tojest.spyOn
a method on an object.requireActual
s withjest.mock
, it's not easy trying to reason about the order in which modules are executed or whether something is referencing the mocked variant of something or the real one.Note that you can do this:
This (usually) works, but the rub is that the above only works because we're using Babel or something to convert the ES exports to an object-like representation. Per the ES6 spec, we shouldn't be able to change a module's exports from outside the module. If/when Jest moves to using native ES modules in Node, it's unclear whether this would still work.
Example
Pitch
Spying on a single export from a module is a pretty common thing to do, especially if you have giant modules full of helper functions and what not. It's also a lot easier to reason about spying on just the one export that thinking about what happens when
jest.mock
replaces the module altogether.There are ways to make this work without having it baked into Jest. But the implementations for something like this would depend heavily on the extent that Jest is relying on CommonJS vs ES modules. Having this be part of Jest itself would add some reassurance that these things were being considered if/when Jest decides to make use of Node's native support for ES modules.
The text was updated successfully, but these errors were encountered: