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

148
Vistas
get value from a Promise for unit-testing

I have a method inside a class calculating the sum of 2 parameters that returns a Promise (a must):

module.exports = class Sum {
  sum(a, b) {
    let c = a + b;
    const myPromise = new Promise(function(myResolve) {
      setTimeout(function(){ myResolve(c); }, 100);
    });
    return myPromise;
  }
}

I use Jasmine framework to unit-test

const MyLocalVariable1 = require('../src/sum'); 
describe('CommonJS modules', () => {
  it('should import whole module using module.exports = sum;', async () => {
    const result = new MyLocalVariable1();
    expect(result.sum(4, 5).then(function(value) {
      return value;
    })).toBe(9);
  });
}

The first expression is what we want to test:

result.sum(4, 5).then(function(value) {
  return value;
})

and the second one is the expected value:

toBe(9)

But how can I get a value from the first expression, because it's a Promise, and expected value of it is [object Promise]. Thanks in advance

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

0

To drive Konrad's point home, you can do the following:

it('should import whole module using module.exports = sum;', async () => {
    const result = new MyLocalVariable1();
    const answer = await result.sum(4, 5);
    expect(answer).toBe(9);
  });

Or the following:

// add done callback to tell Jasmine when you're done with the unit test
it('should import whole module using module.exports = sum;', (done) => {
    const result = new MyLocalVariable1();
    result.sum(4, 5).then(answer => {
      expect(answer).toBe(9);
      done();
   });

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