Stubbing ES modules (imports) does not work for component testing
See original GitHub issueCurrent behavior
using cy.stub
on an ES module does not work as expected, see example code below:
// Person.js
export const getName = () => 'Bob'
// Test which stubs person
import * as person from './Person'
it('Stubs person', () => {
cy.stub(person, 'getName').callsFake(() => 'Billy')
console.log(person.getName()) // Expects 'Billy', gets 'Bob'
})
Cypress 7.5.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Stub an import from a Cypress v10 component test
A user has recently asked in the Cypress Discord channel how to write a component test for a component that executes the following...
Read more >How can I mock the imports of an ES6 module? - Stack Overflow
I'm looking for a way to test Widget with a mock instance of getDataFromServer . If I used separate <script> s instead of...
Read more >How to stub a dependency of a module - Sinon.JS
To stub a dependency (imported module) of a module under test you have to import it explicitly in your test and stub the...
Read more >Unit Testing Challenges with Modular JavaScript Patterns
With native ES modules you cannot stub or spy an exported function made available through the simple named export/import approach because ...
Read more >Manual Mocks - Jest
If you're using ES module imports then you'll normally be inclined to put your import statements at the top of the test file....
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
I am getting the error from cypress “ES Modules cannot be stubbed” when I attempt this.
same to me