-
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
cy.stub breaks after upgrade to 7.3.0 #16532
Comments
|
To further clarify this is happening specifically in component tests. and these component tests are also in a migration from I suspect that the |
Knowing it's with the component testing runner is useful information. The Can you share the contents of |
I tried reproducing as best I could: see here. I could not reproduce - but it's likely the problem is project configuration. In my case, the stub was called. When I copy pasted your example, my IDE gave me deprecation error around the const React = require('react')
const { mount } = require('@cypress/react')
const { CyStub } = require('./CyStub.jsx')
const AuthProvider = require('./auth.js')
describe('cy.stub', () => {
let calls = 0
beforeEach(() => {
cy.stub(AuthProvider, 'login').callsFake(() => {
calls += 1
})
})
it('should stub commonjs require', () => {
mount(<CyStub />)
cy.get('button').click().then(() => {
expect(calls).to.eq(1)
})
})
}) If you can provide a minimal reproduction of the problem you are having, I can take a look. |
Hi @lmiller1990 It's a fork from https://github.com/bahmutov/cypress-react-component-example Stubbing the function //App.js
import { calculate } from './anModule';
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>{calculate()}</p>
</header>
</div>
);
//App.spec.js
import * as anModule from './anModule'
it('renders total is 10', () => {
mount(<App />)
cy.contains(/total is 10/i)
})
it('renders total is mocked value', () => {
cy.stub(anModule, 'calculate').returns('total is 25')
mount(<App />)
cy.contains(/total is 25/i)
}) |
i am also seeing a similar problem and have documented it here |
I tried following this official example for mocking imports by adding // craco.config.js
module.exports = {
babel: {
plugins: [
[
'@babel/plugin-transform-modules-commonjs',
{
loose: true,
},
],
],
},
}; And ended up getting this error for basically every module in my application:
Link to example: |
There error you are describing is actually a limitation of Create React App, see here for more. They talk about the We actually modify the webpack config on the fly to support importing from We definitely need to investigate how we can handle stubbing ES modules. Transpiling to commonjs is one approach, I don't think it's a good long term solution, though. That said, if you do keep going down this route and remove that CRA limitation, you should be able to get this to work. |
Closing this issue. Please follow #22355 for updates on ES module mocking. |
Current behavior
I am migrating a project from 6.5.0 to 7.3.0 The project has a large test suite. After installing 7.3.0 many previously successful tests are failing. After investigation it appears that the behavior of
cy.stub
has changed.The existing tests have many 'module' stubs like:
When the test is run this stub never gets called and the default
AuthProvider.login
function gets called. So the stub does nothing.this stack overflow question demonstrates the same issue https://stackoverflow.com/questions/61944433/how-to-stub-a-module-function-with-cypress
Desired behavior
The behavior should be consistent with 6.5.0 I'm not seeing any documentation on this breaking change to
cy.stub
or how to stub modules like this.Versions
$ node -v
v10.23.0
$ npm -v
6.14.8
$ cypress -v
Cypress package version: 7.3.0
Cypress binary version: 7.3.0
Electron version: 12.0.0-beta.14
Bundled Node version: 14.15.1
The text was updated successfully, but these errors were encountered: