Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
prueba unitaria del componente vue

En mi componente vue, tengo algunos métodos que se disparan en el ciclo de vida montado,

 mounted() { this.GET_WORKFLOW_TYPES() .then(() => { this.GET_WORKFLOW_EVENTS_BY_TYPE({workflow_type: this.audit.selectedWorkflowType}) }); },

Las anteriores son acciones en mi tienda y están asignadas a mi componente de la siguiente manera:

 methods: { ...mapActions([ACTION_TYPES.GET_WORKFLOW_TYPES, ACTION_TYPES.GET_WORKFLOW_EVENTS_BY_TYPE]); }

Quiero probar que ambas funciones se llaman en mi prueba de unidad, así que lo intenté,

 it('Should render the audit component and it\'s children', async () => { wrapper = mount(AdminAudit, { store, localVue}); //Does it retrieve the data we need expect(actions.GET_WORKFLOW_TYPES).toHaveBeenCalled(); await wrapper.vm.$nextTick(() => { expect(actions.GET_WORKFLOW_EVENTS_BY_TYPE).toHaveBeenCalled(); }); })

pero obtengo la siguiente respuesta,

console.error ../../node_modules/vue/dist/vue.runtime.common.dev.js:1893 JestAssertionError: expect(jest.fn()).toHaveBeenCalled()

 Expected number of calls: >= 1 Received number of calls: 0

¿Cómo hago para probar las llamadas asíncronas encadenadas?

Como referencia, estamos usando la versión 2.6.14 de vue

over 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Las acciones de Vuex se programan en microtareas después de que se haya montado el componente, pero su primera aserción se ejecuta antes de que se resuelva la primera acción.

Una solución es esperar hasta el siguiente micro tick:

 const wrapper = shallowMount(MyComponent, { store, localVue }) await wrapper.vm.$nextTick() 👈 expect(actions[ACTION_TYPES.GET_WORKFLOW_TYPES]).toHaveBeenCalled() await wrapper.vm.$nextTick() 👈 expect(actions[ACTION_TYPES.GET_WORKFLOW_EVENTS_BY_TYPE]).toHaveBeenCalled()

Alternativamente, puede esperar hasta la siguiente marca de macro , momento en el que las micro tareas actuales ya se habrán completado:

 const wrapper = shallowMount(MyComponent, { store, localVue }) await new Promise(r => setTimeout(r)) 👈 expect(actions[ACTION_TYPES.GET_WORKFLOW_TYPES]).toHaveBeenCalled() expect(actions[ACTION_TYPES.GET_WORKFLOW_EVENTS_BY_TYPE]).toHaveBeenCalled()

manifestación

over 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Virtual assistant