Next.js Discord

Discord Forum

Unable to extract jest mock function

Unanswered
Tonkinese posted this in #help-forum
Open in Discord
TonkineseOP
Hey guys, why am I not able to extract and import the following mock function from jest.mock:
jest.mock("next-auth/react", () => {
  const originalModule = jest.requireActual('next-auth/react');
  const mockSession = {
    expires: new Date(Date.now() + 2 * 86400).toISOString(),
    user: { username: "admin" }
  };
  return {
    __esModule: true,
    ...originalModule,
    useSession: jest.fn(() => {
      return {data: mockSession, status: 'authenticated'}  // return type is [] in v3 but changed to {} in v4
    }),
  };
});


It gives me some Can not access before initialization error:
import { MyMock } from './helpers'

jest.mock('next-auth/react', MyMock)

1 Reply

TonkineseOP
pretty niche question i guess