cypress-io/cypress-react-unit-test

How to make sure that the next/router is tested or mocked

itstheandre opened this issue ยท 3 comments

I am trying to run a test on my signup page.
I have tried looking into it in a e2e test and it checks that my next/router works becuase I know that after a successful login the page is redirected.

The question is, how do I test it in my component testing?
Because, according to my code coverage report its the only part that is missing to be tested.

Great great work on this!

you can see my repo for an idea:
https://github.com/itstheandre/cypress-test-react-learning/tree/next

Ok, this is a problem with the router returned from useRouter - it actually swallows the error here

I have forked the repo to show the problem: https://github.com/bahmutov/cypress-test-react-learning/blob/master/next-cypress/cypress/components/signup.spec.jsx#L24

The test fails at the end when the component calls router.push('/')

Screen Shot 2020-09-28 at 9 03 14 AM

Seems router = useRouter() returns null

This might be related to #452 that also has router

Hey @bahmutov do you think it would be possible to mock the import? sort of what jest.mock does?

๐ŸŽ‰ This issue has been resolved in version 4.15.0 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€