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

169
Views
Jest unit test cases for NodeJs

I am new to Node.js and Jest unit test cases. I have tried to call Jest for controller.js. It is working for one endpoint for another endpoint the controller is making a call to service.

I have not used the mock. Direct call from Jest I have tried. But 'request.on' has not been covered from Jest. I tried all combinations but no luck. I am adding the code details below.

Please let me know how to call 'request.on' from Jest or how to make calls to Service using mock.

controller.js

'use strict'

const service = require('./service/serviceClass');

const control = {
  serve: (req, res) => {
    service.serve(req, res);
  },
}
module.exports = control;

serviceClass.js

require("dotenv").config();
const firebase = require('firebase-admin');
let fbMap = new Map();

function fbInit(){
   let fbAdmin = firebase.initializeApp({
      credential: firebase.credential.cert('service.json');
   });
   fbMap.set('fb', fbAdmin);
}

let serve = {

    serve: (req, res, next) => {
       fbInit();
       const body = [];
       req.on('data', (chunk) => body.push(chunk))
       req.on('end', () => {
             const reqString = Buffer.concat(body).toString();
             var id = JSON.parse(reqString).firebaseId;
             if(id !=== undefined){
            var fire = fbMap.get('fb');
                  fire.auth().getUser(id).then((user)=>{
                    if(user != null){
                     console.log("email of the user:"+user.email);
                 }
             )};
          }
        } 
    }
}

module.exports=serve;

It would be great if I get the help to either make 'mock' calls for service or how to make the code cover for 'request.on' from Jest.

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!