Stubbing methods doesn't work
See original GitHub issueCurrent behavior
My component is as follows
import {myMethod} from "./myFile"
function MyComponent(){
const output = myMethod();
return <div>{output}</div>
}
And i am stubbing myMethod because I want it to return a specific value like so
import * as Parent from "./myFile"
cy.stub(Parent, "myMethod", () => "i was stubbed")
cy.visit("/my_component") # Does not use the stubbed function
but then i am finding that the component still uses the original function and not the stubbed function, does anyone know what i am doing wrong?
Desired behavior
No response
Test code to reproduce
# Component file
import {myMethod} from "./myFile"
function MyComponent(){
const output = myMethod();
return <div>{output}</div>
}
# Test file
import * as Parent from "./myFile"
cy.stub(Parent, "myMethod", () => "i was stubbed")
cy.visit("/my_component") # Does not use the stubbed function
Cypress Version
8.7.0
Other
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Mockito stubbing is not working when service class method is ...
I tried to stub a set of data suppose to be return by a specific service method to a method present in controller...
Read more >Stubbing and Mocking with Mockito and JUnit - Semaphore CI
Learn how to create true unit tests by mocking all external dependencies in your JUnit classes with the help of Mockito.
Read more >`void` methods | Migrating from Mockito | MockK Guidebook
Mockito's when method doesn't work with void methods. To create a stub that doesn't return anything, the doNothing method is used.
Read more >A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
After the test, you can query the mock to see what specific methods were ... AClass mock = mock(AClass.class); //This doesn't work. when(mock.call("a", ......
Read more >Mock / stub private method does not work, weird
misusing.MissingMethodInvocationException : when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We used CRA to create our project and didn’t want to eject. I had a similar issue and I fixed it when I found a fix from another issue, I also wanted to add code coverage output so my config also includes that - you can remove if it’s not required in your setup
/plugins/index.js
The main fix is adding this to your babel config:
Stubbing modules isn’t working in the component runner. The same thing is ok in the e2e runner. Wait for Cy 10 or work around it like the above…