I'm trying to test my component in cypress. the component uses the useRouter hook from next/navigation in a button to push back to another page.
import { useRouter } from "next/navigation";
import { VStack, Heading, Text, Button, Box, HStack } from "@chakra-ui/react";
const ForgotPassword = () => {
const [usersEmails, setUsersEmails] = useState<String[]>([]);
const [email, setEmail] = useState("");
const [error, setError] = useState(false);
const router = useRouter()
useEffect(() => {
const fetchEmails = async () => {
const { error, data } = await supabase.from("users").select("email");
if (error) {
console.log(error);
} else {
const emailArray = data.map((user) => user.email);
setUsersEmails(emailArray);
}
};
fetchEmails();
}, [supabase]);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
};
return (
<HStack h="100vh">
<Box>
<VStack w="full" maxW="md">
<Heading as="h3" size="lg" color="#006bb2" alignSelf="flex-start">
Forgot Password
</Heading>
<Text mb="15px">
Enter your email and we"ll send a link to reset your password
</Text>
<form onSubmit={handleSubmit}>
<input
type="email"
id="email"
name="email"
placeholder="[email protected]"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
{error && (
<Text mt="5px" color="red">
We cannot find your email
</Text>
)}
<button>
Submit
</button>
</form>
<Button
leftIcon={<ArrowBackIcon />}
color="gray.500"
_hover={{ color: "#006bb2", cursor: "pointer" }}
variant="link"
onClick={() => router.push("/login")}
>
Back to Login
</Button>
</VStack>
</Box>
</HStack>
);
};
export default ForgotPassword;
when i mount the component, i get the error from cypress
(uncaught exception)Error: invariant expected app router to be mounted
The following error originated from your application code, not from Cypress. invariant expected app router to be mounted
but if i comment out the "import useRouter", "const router = useRouter()" line and remove the "router.push("/login")", then the component mounts and cypress doesnt throw any error
i've tried the next/router & next/link solution from cypress docs
import React from 'react'
import ForgotPassword from './page'
import Router from 'next/router'
describe('Testing the forgot password component', () => {
context('stubbing out `useRouter` hook', () => {
let router
beforeEach(() => {
router = {
push: cy.stub().as('router:push')
}
cy.stub(Router, 'useRouter').returns(router)
})
it('checks if user email exits in db', () => {
cy.mount(<ForgotPassword />)
cy.get('input[name="email"]').type("[email protected]");
// cy.get('button').click()
// cy.get('@routerBack').should((mock) => {
// expect(mock).to.have.been.calledOnce
// })
})
})
})
cypress still throws the same "(uncaught exception)Error: invariant expected app router to be mounted" error and the component still doesn't mount.
i changed the line import Router from 'next/router' to import Router from 'next/navigation' and cypress error changes to
Trying to stub property 'useRouter' of undefined
Because this error occurred during a before each hook we are skipping the remaining tests in the current suite: stubbing out useRouter hook
