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

124
Vistas
jasmine spy failing when being consumed by a promise
// CODE

var catalogDOM = (function() {
  return {
    updateAvailabilityForItem: function(quant) {
      console.log(">>>> in updateAvailabilityForItem")
    },
    syncAvailability: function() {
      return new Promise(function(resolve, reject) {
        ...
      })
    }
  }
})();

$(document).on("click", ".item_info", function() {
  console.log(">> calling sync")
  catalogDOM.syncAvailability()
  .catch(function() {
    console.log(">>> caught")
    catalogDOM.updateAvailabilityForItem(0)
  })
})

// SPEC

describe("click on item_info", function() {
  beforeEach(function() {
    info_btn = affix(".item_info")

    spyOn(catalogDOM, "updateAvailabilityForItem")
    syncFunction = spyOn(catalogDOM, "syncAvailability")
    syncFunction.and.returnValue(new Promise(function(resolve, reject) {reject()}))
    info_btn.click()
  })
  it("should call updateAvailabiliytForItem", function() {
    expect(catalogDOM.updateAvailabilityForItem).toHaveBeenCalledWith(0)
  })
})

// CONSOLE OUTPUT

>> calling sync
>>> caught
>>>> in updateAvailabilityForItem

The above spec is failing, moreover, I can see via console, see messages above, that catalogDOM.updateAvailabilityForItem is actually being called through... is there something about the promise set up that negates the spy?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issue is that the promise is happening asynchronously (at a later point in time) and when you expect, it is too early.

For a quick unblock, you can wrap you expectation in a setTimeout which will lower the priority of what's happening in the setTimeout to after the promise but I am not sure if this is a good approach.

// add done to function argument
it("should call updateAvailabiliytForItem", function(done) {
    setTimeout(() => {
      expect(catalogDOM.updateAvailabilityForItem).toHaveBeenCalledWith(0);
      // call done to let Jasmine know your asynchronous work is done.
      done();
    }, 100);
  })

You can read more about testing asynchronous stuff in Jasmine here: https://jasmine.github.io/tutorials/async

I usually use async/await for asynchronous tasks.

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