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

155
Vistas
test http.request with options using jest

Im learning using Jest and i faced this function, but not sure how to start testing, it has inside an http.request({}, (res) => {})

and not sure how can i mock this http.request inside the current function: also the resolve and reject from the current promise,

in advance Thank You!

const options = {
hostname: 'myHostName'
port: 'myPort',
path: 'myPath',
method: 'GET'
};

function myRequest(options) {
  return new Promise((resolve, reject) => {
    
    const req = http.request(options, (res) => {

      if (res.statusCode === 200) {
        let responseData = '';

        res.on('data', (data) => {
          responseData += data;
        });

        res.on('end', () => {
          const response = JSON.parse(responseData);

          resolve([response, true]);
        });
      } else {
        reject(`res.message`);
      }
    });
    req.on('error', (error) => {
      reject(error);
    });
    req.end();
  });
}

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