Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!