authts/react-oidc-context

Unable to mock useAuth with jest

raghavi92 opened this issue · 2 comments

I went through this already existing issue:
#372

But my mock for the useAuth function is simply not working. Can someone help please ?

I added the below code segment to my test file:

const auth = require('react-oidc-context');
jest.mock('react-oidc-context', () => {
  const orig = jest.requireActual('react-oidc-context');
  return {
    __esModule: true,
    default: jest.fn(),
    useAuth: jest.fn(),
    ...orig,
  };
});
  auth.useAuth.mockResolvedValue('test');
  render(<App />, {
    wrapper: ({ children }) => (
      <AuthProvider
        authority='authority'
        client_id='client'
        redirect_uri='redirect'
      >
        {children}
      </AuthProvider>
    ),
  });

  waitFor(() => {
    expect(
      screen.getByText('Tournaments managed by Lotus Chess Academy')
    ).toBeInTheDocument();
  });
});

I'm getting the error TypeError: auth.useAuth.mockResolvedValue is not a function.
I have a nextjs project with typescript enabled.

With auth.useAuth.mockResolvedValue('test'); you mock useAuth like it would be a string ("test"), but it is a function...

@pamapa Actually I tried this first but it didnt work:

jest.mock('react-oidc-context', () => {
  const orig = jest.requireActual('react-oidc-context');
  return {
    __esModule: true,
    default: jest.fn(),
    useAuth: jest.fn,
    ...orig,
  };
});

I might have pasted my last snippet out of a lot of failed attempts...