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

128
Vistas
How to avoid time in a test when expecting a Promise with setTimeout in Jasmine

Given I have the following code:

  public returnData(): Promise<any> {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: true,
        });
      }, 2000);
    });
  }

Now, currently my unit test successfully do:

  it('should return data', async function () {
    const responseMock = {
      data: true,
    };

    await expectAsync(component.returnData()).toBeResolvedTo(responseMock);
  });

But there is an obvious problem: the test takes more than 2000ms, which is unacceptable. I want to be able to test this without the need of waiting the time. I've tried many different solutions, like the following:

  it('should return data', async function () {
    jasmine.clock().install();

    const responseMock = {
      data: true,
    };

    setTimeout(() => {
      expectAsync(component.returnData()).toBeResolvedTo(responseMock);

      jasmine.clock().tick(2000);
    }, 2000);

    jasmine.clock().uninstall();
  });

The test above pass with great timing, but when I change the responseMock value for anything else, it still pass, so it's not really working...

What's the key to test this specific case, without the need of waiting the setTimeout time?

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

0

Why are you running the expectations in a setTimeout? Maybe we don't need that.

Try this:

it('should return data', async function () {
    jasmine.clock().install();

    const responseMock = {
      data: true,
    };

    // call the method that returns a promise and have a handle on its promise
    const promise = component.returnData();

    // make 2 seconds pass in a fake way
    jasmine.clock().tick(2000);

    // await the promise
    const result = await promise; 

    // assert the result
    expect(result).toEqual(responseMock);
    
    jasmine.clock().uninstall(); 
  });

I see that you're using component. and most likely you're using Angular. If you're using Angular, I use fakeAsync/tick for time and promises.

Check out fakeAsync/tick here.

// 
it('should return data', fakeAsync(() => {
    const responseMock = {
      data: true,
    };

    // call the method that returns a promise and have a handle on its promise
    const promise = component.returnData();

    // make 2000 pass in a fake way
    tick(2000);

    promise.then(result => {
      expect(result).toEqual(responseMock);
      expect(1).toBe(2); // delete this - make sure the test fails to know we made it here
    });

  }));
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