Next.js Discord

Discord Forum

jest.mock not working

Unanswered
Philippine Crocodile posted this in #help-forum
Open in Discord
Philippine CrocodileOP
Hey everyone I have a fresh NextJS app created, and I am importing from '@/queries/auth' in my component. I have set up Jest using the docs here: https://nextjs.org/docs/pages/building-your-application/testing/jest#manual-setup and tests run fine, until I try to use jest.mock which fails to actually stop the underlying code from running. Any suggestions of what I need to do? I already added this to my jest.config:
moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/$1',
  },


Here is a snippet of my test using the mock:
describe('LoginForm', () => {
  test('renders login form correctly', () => {
    // Mock the login function
    const mockLogin = jest.fn();
    jest.mock('@/queries/auth', () => ({
      _esModule: true,
      useLogin: () => ({
        mutateAsync: mockLogin,
      }),
    }));
    render(<LoginForm />);

0 Replies