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

179
Visualizações
How to mock a function/method in Vue 3 with Vue Test Utils 2?

I am in the process of converting my project from Vue 2 to 3 and also testing library to Vue test utils 2. Before, mocking methods works like magic:

export default {
  methods: {
    greet(text) {
      sayHello('hello world')
    },
    sayHello(text) {
      console.log(text)
    }
  }
}
test('should sample', async () => {
  const spy = jest.spyOn(wrapper.vm, 'sayHello')
  wrapper.vm.greet('hello world')
  expect(spy).toHaveBeenCalledWith('hello world')
})

I tried converting it to composition api:

export default defineComponent({
  setup() {
    function greet(text) {
      sayHello(text)
    }

    function sayHello(text) {
      console.log(text)
    }

    return {
      greet,
      sayHello
    }
  }
})

and now testing is not working at all

expect(jest.fn()).toHaveBeenCalledWith(...expected)

Expected: "hello world"

Number of calls: 0

How do I mock functions that call another functions in Vue 3 + Vue test utils 2?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you should create your spy functions before the wrapper instance is created (where you use mount or shallowMount). Creating spies before creating instance and using

beforeEach(() => {
  jest.resetAllMocks();
})

in order to reset mocks for following cases should do the trick.

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