Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda