Hello everyone, after I have my next.js 13 app ready I start with jest testing. I have all necessary dependencies installed and want to begin with a test for my register functionallity. In my register page I use useRouter to push the user after registration to the login page. Here begins the first issue and I have read everything I found in the net, but not found a solution. This is my test:
jest.mock('next/navigation', () => ({
useRouter: () => ({
push: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn(),
emit: jest.fn(),
},
isFallback: false,
}),
}));
describe("Register has all its elements",()=>{
it("Find all inputs in the register",()=>{
render(<Register/>)
const vorname = screen.getByRole("textbox",{
name:"vorname"
})
const nachname = screen.getByRole("textbox",{
name:"nachname"
})
const username = screen.getByRole("textbox",{
name:"username"
})
const email = screen.getByRole("textbox",{
name:"email"
})
const password = screen.getByLabelText("Passwort")
const passwordValidation = screen.getByLabelText("Passwort validieren");
expect(vorname).toBeInTheDocument();
expect(nachname).toBeInTheDocument();
expect(username).toBeInTheDocument();
expect(email).toBeInTheDocument();
expect(password).toBeInTheDocument();
expect(passwordValidation).toBeInTheDocument();
})
})
But I have an error when running this test:
invariant expected app router to be mounted
16 | const Register = () => {
> 17 | const router = useRouter();
So I googled and found different comments, but no answer worked for me. The last what I tried was:
jest.mock("next/navigation", () => ({
useRouter() {
return {
prefetch: () => null
};
}
}));
It would be nice when someone could help me out. Thanks