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

337
Vistas
Failed unit test case (JASMINE)

I have a function for which i am trying to write a spec which is failing with the error

Unhandled promise rejection: [object Object]

Function:

someFunction(a,b,c) {
    var dfd = q.defer();
    this.getInfo(b,c).then((data)=> {
       //do Something
    
      dfd.resolve(data);
    }).catch((error)=> {
       dfd.reject(error)
    })
    
    return dfd.promise;
}

spec:

describe( 'SomeFunction', () => {
    it( 'should reject when getInfo request fails',  ( done ) => {
        spyOn( utility, 'getInfo' ).and.callFake( async () => {
            var deferred = q.defer();               
            deferred.reject( { error: 500 } );
            return deferred.promise;
        });
        let promise =  utility.someFunction( a,b,c );
        expect(utility.getInfo).toHaveBeenCalled();
        promise.then().catch( function ( data:any) {
            expect( data ).toEqual( { error: 500 } );
        } ).finally( done );
    });

I want to write to here the test case for reject but then getting the error unhandled promise rejection.

Any one let me know if i am doing anything wrong here.

Any help would be appreciated.

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

0

You should use rejectWith(value):

Tell the spy to return a promise rejecting with the specified value when invoked.

to spy utility.getInfo() method. Since the return value of utility.someFunction is a promise, you should use expectAsync(actual) to create an asynchronous expectation.

index.ts:

import q from 'q';

const utility = {
  someFunction(a, b, c) {
    var dfd = q.defer();
    this.getInfo(b, c)
      .then((data) => {
        dfd.resolve(data);
      })
      .catch((error) => {
        dfd.reject(error);
      });
    return dfd.promise;
  },
  async getInfo(b, c) {
    return 'real data';
  },
};

export { utility };

index.test.ts:

import { utility } from './';

describe('69378465', () => {
  it('should pass', async () => {
    spyOn(utility, 'getInfo').and.rejectWith({ error: 500 });
    await expectAsync(utility.someFunction('a', 'b', 'c')).toBeRejectedWith({ error: 500 });
    expect(utility.getInfo).toHaveBeenCalled();
  });
});

test result:

Test Suites & Specs:

1. 69378465
   ✔ should pass (11ms)

>> Done!


Summary:

👊  Passed
Suites:  1 of 1
Specs:   1 of 1
Expects: 2 (0 failures)
Finished in 0.017 seconds
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