How to mock redux for react-component/unit tests in cypress #14846
Unanswered
JohnHuynh
asked this question in
Component Testing
Replies: 1 comment 3 replies
-
i'm also slamming against this. :( if you ever find a way to do this, i'd love to hear it!! I suspect that cypress might not actually be capable of doing this, as i believe it uses sinon for it's stubbing, and sinon has no control of module imports (see this ticket: sinonjs/sinon#1711) here's the link to my (related) discussion thread |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am using the @cypress/react package to perform unit-testing on react components.
In my case, these react components contain external influence such as services or redux state management.
I tried to stub these external components with cy.stub() but I get an error that I don't know how to work around.
Example:
I have a component that invokes useSelector from redux in order to get the current state currentState. It looks like the following:
In order to test this I created the following spec file:
Running the test with npx cypress open it gives me an AssertionError:
Timed out retrying after 4000ms: Expected to find element: [data-testid=my-component], but never found it.
Apparently, the element that I tried to stub returns a null value, which is why this AssertionError occured. But how do I resolve this?
Maybe I have not understood the stubbing functionality fully yet, but after days of research I haven't found anything helpful. Any help would be much appreciated.
Beta Was this translation helpful? Give feedback.
All reactions