Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda