Try to Make a test sign up, I got this: FAIL tests/signUp.test.js (6.256 s) Test signUp × Renders the SignIn component (181 ms)
● Test signUp › Renders the SignIn component
No router instance found.
You should only use "next/router" on the client side of your app.
21 |
22 | useEffect(() => {
> 23 | if (!localStorageUser && !_.includes(withoutUserRouters, Router.pathname)) {
| ^
24 | Router.push(routers.signin);
25 | }
26 | setUser(localStorageUser);
at getRouter (node_modules/next/client/router.ts:122:11)
at Object.pathname (node_modules/next/client/router.ts:84:22)
at context/AuthContext.js:23:69
This is The test file code:
import {fireEvent, render} from '@testing-library/react';
import '@testing-library/jest-dom';
import SignUp from '../pages/signUp';
import {AuthProvider} from '../context/AuthContext';
import {UIProvider} from '../context/UIContext';
import {SnackbarProvider} from 'notistack';
const useRouter = jest.spyOn(require('next/router'), 'useRouter');
useRouter.mockImplementation(() => ({
pathname: '/',
}));
describe('Test signUp', () => {
it('Renders the SignIn component', () => {
const {getByText} = render(
<SnackbarProvider>
<AuthProvider>
<UIProvider>
<SignUp />
</UIProvider>
</AuthProvider>
</SnackbarProvider>,
);
const button = getByText(/Sign Up/i);
expect(button).toBeInTheDocument();
fireEvent.click(button);
});
});
at context/AuthContext.js Router imported like so:
import Router from 'next/router';
and then There is a useEffect like so:
useEffect(() => {
if (!localStorageUser && !_.includes(withoutUserRouters, Router.pathname)) {
Router.push(routers.signin);
}
setUser(localStorageUser);
}, [localStorageUser]);
Why is the mock for Router not take place?