-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Stubbing ES modules (imports) does not work for component testing #17048
Comments
I'm getting Billy when I run this code. Could you provide a repo that reproduces the issue?
export const getName = () => 'Bob'
// Test which stubs person
import * as person from './person'
it('Stubs person', () => {
cy.stub(person, 'getName').callsFake(() => 'Billy')
expect(person.getName()).to.eq('Billy') // Expects 'Billy', gets 'Bob'
}) |
I'm getting the same problem. @jennifer-shehane but do you get the result inside the app that is using person? What configuration do you have? browserify or webpack? |
@jennifer-shehane just wanted to bump this as essentially stubbing / spying does not work in the context of unit testing. |
So just bumping this again - essentially stubbing module imports ( |
Update - I finally managed to get this working reasonably with component tests (unit testing). The core concepts are actually in a code example I didn't find until now (my bad): |
I do not think this is resolved, Cypress should have a viable solution builtin |
I am getting the error from cypress "ES Modules cannot be stubbed" when I attempt this. |
same to me |
same problem. for me, it's much easier to stub the function that retrieves values from localStorage than to fill localStorage |
To anyone stumbling on this issue the latest information on this topic can be found in #22355 The TL;DR is that ESM mocking is very different than CommonJS mocking - the ESM spec makes it almost impossible to use standard mocking mechanisms which makes things difficult for Cypress (and any other tool that runs in the browser). We have recently released an experimental plugin that can be applied to your test Vite config to enable common mocking scenarios, but it is an early Alpha release so there will likely be hiccups and customization necessary to get it working in your project. The linked issue above has several suggestions for ways to either restructure your code or restructure your tests to mitigate the need for mocking/spying. |
@mike-plummer we've got a NextJS application and are trying to do ESM mocking.. that issue is referencing a vite plugin. Any thoughts for NextJS? |
@dwilt It should be relatively simple to update your Next application's Webpack Config to specify the module output format. Have you customized it to output ESM modules? I thought Webpack was still outputting commonjs by default, but maybe that's changing. Take a look at the |
Current behavior
using
cy.stub
on an ES module does not work as expected, see example code below:Cypress 7.5.0
The text was updated successfully, but these errors were encountered: