question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stubbing ES modules (imports) does not work for component testing

See original GitHub issue

Current 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
bvandercar-vtcommented, Aug 31, 2022

I am getting the error from cypress “ES Modules cannot be stubbed” when I attempt this.

0reactions
binvbcommented, Oct 8, 2022

I am getting the error from cypress “ES Modules cannot be stubbed” when I attempt this.

same to me

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found