I'm trying to mock out useAuth0 from @auth0/auth0-react and am running into issues returning a mock value. I currently have a simple provider that uses useAuth0
import React, { createContext } from 'react';
import { useAuth0 } from "@auth0/auth0-react";
export const UserContext = createContext();
export const UserProvider = props => {
const {
isLoading
} = useAuth0();
return (
props.children
)
};
This provider is being used in a custom render function in my tests
// test-utils.js
import React from 'react'
import { render as rtlRender } from '@testing-library/react'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import { MockedProvider } from '@apollo/client/testing';
// Import your own reducer
import applicationsReducer from "../app/slices/applications.slice";
import { UserProvider } from "../user-context";
function render(
ui,
{
initialState,
mock,
store = createStore(applicationsReducer, initialState),
...renderOptions
} = {}
) {
function Wrapper({ children }) {
return <Provider store={store}>
<MockedProvider mocks={mock} addTypename={false}>
<UserProvider>{children}</UserProvider>
</MockedProvider>
</Provider>
}
return rtlRender(ui, { wrapper: Wrapper, ...renderOptions })
}
// re-export everything
export * from '@testing-library/react'
// override render method
export { render }
To mock it I've set up a mocks folder using the scoped module documentation from jest
--src
--__mocks__
--@auth0
--auth0-react.js
The code for that mock looks like this
export const withAuthenticationRequired = jest.fn().mockImplementation((component, _) => {
return component
})
export const useAuth0 = jest.fn().mockReturnValue({
error: null,
isAuthenticated: true,
isLoading: true,
user: {},
})
The withAuthenticationRequired mock works fine, however the useAuth0 keeps throwing the error
Error: Uncaught [TypeError: Cannot destructure property 'isLoading' of '(0 , _auth0React.useAuth0)(...)' as it is undefined.]
I've been struggling with this for a few hours now, and any help would be appreciated.