How do I mock out useAuth0 for a Vitest?

I am using Auth0-Vue with Vite. Everything works great except for testing. When I run I get…

TypeError: Cannot destructure property ‘isAuthenticated’ of ‘useAuth0(…)’ as it is undefined.

I can hack fix it by changing it to something like…

const auth0 = ref(useAuth0());
const value = ref(0);
const isAuthenticated = computed(()=>{
  return !!auth0?.value?.isAuthenticated;
});
beforeAll(()=>{
    loginWithRedirect = vi.fn().mockImplementation(onLogin);
    logout = vi.fn().mockImplementation(onLogout);
  });
wrapper.vm.$.setupState.auth0 = {
        isAuthenticated: true,
        logout,
      };

But this seems and looks super hacky. Am I missing something easy to mock out the useAuth0 function?

Found the answer here

1 Like

thanks, this saved my time!