-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
Unable to mock React useState #2069
Comments
We are trying to keep the GitHub issues list tidy and focused on bugs and feature discussions. This ticket looks like a usage question; please post it to StackOverflow and tag it with If you feel that your topic is an issue with Sinon, please open a new ticket and follow the guidelines for reporting an issue. |
I did - but I don't understand how this doesn't fall under the guidelines for reporting issue? From my understanding Sinon isn't working as intended in this use case based on the stubbing documentation and previous cases where similar code works fine for stubbing. If it's working as intended it's unclear as to why. |
There is nothing that says Sinon is doing anything wrong here. And there are multiple ways the example could go wrong and they all relate to binding. Meaning that you probably bind the reference to useState in your client code before you stub out the export in the test. This relates to module binding, probably using Babel and/or Webpack, which is not what this library is about. We do have multiple guides though, and I would try out the link levels replacement strategy first ("link seams"): This should work fine when running through Babel-Node, but if you also employ Webpack you'll encounter complications, which is out of scope (as the various Webpack versions have different ways of exporting modules). |
P.S. Post a link to the stackoverflow question and I'll try to find time in making a runnable example project. |
Here's a runnable demo where I stub out You don't strictly need Sinon to demonstrate this, though. Just overwriting the exported reference would be enough. I don't have access to a development environment on holiday, so I just coded it up in the browser using repl.it, and as I don't know how to configure it using Babel and all that jazz it is purely standard EcmaScript, but you should be able to follow as it's pretty close to the original JSX. |
Really appreciate the example and the explanation! |
No worries - at least now we can refer people to an example :) BUT should it for some reason NOT work in your case, I assume it's due to some bundler/packer configuration issue, most probably Webpack. In that case, you might be interested in reading #1762 or the associated webpack thread webpack/webpack#6979 (comment) to understand why Sinon is not responsible for this and what options you have to achieve your target. Izhaki has some good input. |
How would you handle multiple I bet if you mock out useState for a boolean value, it'll set Still haven't found out how to mock useState effectively. |
@littleh322 That's a usage question and we are not equipped (i.e. payed) to handle general support.; please post it to StackOverflow and tag it with |
Describe the bug
I'm unable to use Sinon to mock the React hook
useState
. I've tried multiple different ways but always end up with it not being mocked.My code looks like this:
And using Sinon in my test I've tried to stub it like so:
Logging out
useState
in my function when my test runs seems to reveal that it never actually gets mocked by Sinon:[Function: useState]
and I'm unable to callsetExpandedStub.args
etc.To Reproduce
Steps to reproduce the behavior:
React.useState
.Expected behavior
I expect the function to be stubbed so I can tell when it has been called.
Context (please complete the following information):
7.3.2
Mac OSX
React, Enzyme, Tape
Additional context
https://reactjs.org/docs/hooks-state.html
The text was updated successfully, but these errors were encountered: