Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to mock useAuth with jest #1290

Open
raghavi92 opened this issue Jun 24, 2024 · 2 comments
Open

Unable to mock useAuth with jest #1290

raghavi92 opened this issue Jun 24, 2024 · 2 comments
Labels
question Further information is requested

Comments

@raghavi92
Copy link

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.

@pamapa pamapa added the question Further information is requested label Jun 24, 2024
@pamapa
Copy link
Member

pamapa commented Jun 24, 2024

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

@raghavi92
Copy link
Author

raghavi92 commented Jun 26, 2024

@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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants