breezy-plastic-64948
01/30/2023, 10:41 PMuseHistory hook of react-router-dom which is being imported and used by the component I am testing.
This is what i am trying to do without success:
import * as RRDom from 'react-router-dom';
describe('My test', () => {
it.only('test', () => {
cy.stub(RRDom, 'useHistory').returns({
push: () => {
console.log('push stubbed!');
},
});
cy.mount(<MyComp />);
cy.contains('Click me').click();
});
})
And here the simple Component
import { useHistory } from 'react-router-dom';
const MyComp = () => {
const history = useHistory();
return (
<div onClick={() => history.push('/somewhere')}>
Click me
</div>
}
This code throw an error when running the test: ES Modules cannot be stubbed.
We are currently using Vite.
I don't want to touch the existing Component implementation by changing imports or whatsoever.
We have choosen Cypress Component Testing instead of React Testing Library but this limitation that seems unsolvable is a main concern, please let me knowbreezy-plastic-64948
02/24/2023, 1:41 PM