https://cypress.io logo
ES Modules cannot be stubbed
# i-need-help
b
Hello, In my Component Test I need to stub the
useHistory
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:
Copy code
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
Copy code
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 know
Up
3 Views