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

131
Vistas
How can I test this function?

I have the following function which I can't test. I'm using Jest framework.

add.js

exports.sum = async function(flag, first, second) {
    let utility;
    if(flag) {
        utility = require('./newValue');
    } else {
        utility = require(Runtime.giveFunctions()['utility'].path);
    }

    const third = await utility.getThirdValue(first);

    //....
}

newValue.js

function getThirdValue(first) {
    return new Promise((resolve, reject) => {
        if(first % 2 === 0) {
            resolve(first);
        } else {
            console.log("Error");
            reject(null);
        }
    });
}
module.exports = {
    getThirdValue,
};

When I pass flag === true (in add.js) the test work correctly (because I require the original newValue.js file). When I try to pass flag === false the test fail and I receive the error 'utlity.getThirdValue is not a function'. This error appears when the function (add.js) call 'utility.getThirdValue(first)'. I think because there is no function when I mock Runtime.giveFunctions['utility'].path. In my test I write these rows:

const Runtime = {
    giveFunctions: jest.fn().mockReturnValue({
        utility: jest.fn().mockReturnValue({
            path: jest.fn()
        })
    })
};

window.Runtime = Runtime;

I can't use ES6, for this I'm searching a solution without 'import' statement. In my test I also tried to use jest.mock or jest.spyOn but the test still doesn't work. How can I solve? Thank's to all.

about 4 years ago · Juan Pablo Isaza
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