How to test components that use useOktaAuth

Recently I upgrade the react-okra library and have transitioned the app to use the new hooks. I am updating my tests now and this is what is happening:

const { authState, authService } = useOktaAuth();

// TypeError: Cannot destructure property `authState` of 'undefined' or 'null'

In order to fix that, I tried mocking the hook by doing:

    jest.mock('@okta/okta-react', () => ({
      useOktaAuth: () => {
        return {
          authState: {},
          authService: {}
        };
      }
    }));

That isn’t working. Any ideas on how to test these components?

Thanks

Can somebody help with this? I have the same problem. I used the code in another part of the code and it’s working.

I had the similar problem. My answer is here

Hope it will save you.