Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

419
Visualizações
Vue: How to mock Auth0 for testing with Vitest

I am trying to test a Vue component with Vitest but in order to do that I need to mock auth0

Below is my Navbar.test.ts file, however when I run the test I keep getting the following error Cannot read properties of undefined (reading 'mockReturnValue' as useAuth0 seems to be undefined even though I am imported it at the top of the page. Maybe I'm just not understanding the mocking side of it very well but any help would be appreciated.

import { vi } from 'vitest'
import { ref } from "vue";
import { shallowMount } from '@vue/test-utils'
import { useAuth0 } from '@auth0/auth0-vue';
import NavBar from "@/components/NavBar.vue";

const user = {
    email: "user@test.com",
    email_verified: true,
    sub: ""
};

vi.mock("@auth0/auth0-vue");

const mockedUseAuth0 = vi.mocked(useAuth0, true);

describe("NavBar.vue", () => {
    beforeEach(() => {
        mockedUseAuth0.mockReturnValue({
            isAuthenticated: ref(true),
            user: ref(user),
            logout: vi.fn(),
            loginWithRedirect: vi.fn(),
            ...
            isLoading: ref(false),
        });
    });

    it("mounts", () => {
        const wrapper = shallowMount(NavBar, {
            props: { },
        });

        expect(wrapper).toBeTruthy();
    });

    afterEach(() => vi.clearAllMocks());
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

vi.mocked() is just a helper for TypeScript support. It doesn't mock anything.

The following changes fixes your test:

  1. Don't use a named import of the module in your test. Instead, import the whole module.

  2. Remove vi.mocked(), as this has no effect.

  3. Using the import from step 1, add a useAuth0 property that is set to a mock function, returning a mock of loginWithRedirect.

// NavBar.spec.js
import { describe, it, expect, vi } from 'vitest'
import NavBar from '../NavBar.vue'
import { shallowMount } from '@vue/test-utils'

1️⃣
// import { useAuth0 } from '@auth0/auth0-vue' // DON'T DO THIS
import * as auth0 from '@auth0/auth0-vue'

vi.mock('@auth0/auth0-vue')

2️⃣
// const mockedUseAuth0 = vi.mocked(useAuth0, true)

describe('NavBar', () => {
  it('calls useAuth0 > loginWithRedirect', async () => {
    const loginWithRedirect = vi.fn()

    3️⃣
    auth0.useAuth0 = vi.fn().mockImplementation(() => ({
      loginWithRedirect,
    }))

    const wrapper = shallowMount(NavBar)
    await wrapper.find('button').trigger('click')
    expect(auth0.useAuth0).toHaveBeenCalled()
    expect(loginWithRedirect).toHaveBeenCalled()
  })
})

demo

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda