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

201
Views
How to send mocked request using chai http

I am fairly new to integration testing and I am trying to send a mock request using mocha and chai http but having no luck. I want to try and have a jwt verified by passing in a mocked token which I create, however, all of the data is being passed into the req.body.

Is there a way of sending the entire req instead of just passing data in the req.body through the .send()?

// TEST I AM RUNNING
it('should POST /newCertificate for an Accreditor ',async()=>{
    let status,
        json,
        res;

    status = stub();
    json = spy();
    res = { json, status };
    status.returns(res);


    let title = 'title';
    let recipientEmail = 'student@email.com';
    let grade = 'grade';
    let type = 'type';
    let department = 'department';
    const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYyNDFhMjhlY2Y4Y2FlOWFiYmQ4Nzg5NSIsImlhdCI6MTY0ODQ2ODYyMiwiZXhwIjoxNjQ4NzI3ODIyfQ.KU6Un3YPq79TRruTrM8y2BrvgRH8xOn882LVy6vwhU4'
    const body = JSON.stringify({ title, recipientEmail, grade, type, department })
    const req = mockRequest(token, body);


    let res2 = await chai
      .request(app)
      .post('/newCertificateRoutes/newCertificate')
      .set('content-type', 'application/x-www-form-urlencoded')
      .send(req) // I AM TRYING TO SEND THE ENTIRE req but it only adds req to req.body

    expect(res2.status).toHaveBeenCalledWith(201);

});
// FUNCTION I AM CALLING
module.exports.newCertificate_post = async (req, res) => {

        let { title, recipientEmail, grade, type, department }  = req.body;
        const token = req.cookies.jwt;
        let accreditorId = null;
        let departmentId = null;

        console.log("token: ", token);
        console.log("req.body: ", req.body);


        // check if department or accreditor/admin is issueing degree
        jwt.verify(token, 'ultralongsecretkeywhichnooneisabletoguessaccreditor', (err, decodedToken) => {
            if (err) {
                jwt.verify(token, 'ultralongsecretkeywhichnooneisabletoguessdepartment', (err, decodedToken) => {
                    if (err) {
                        console.log(err.message);
                    } else {
                        departmentId = decodedToken.id
                    }
                });
            } else {
                accreditorId = decodedToken.id;
            }
        });

        console.log("departmentId: ", departmentId);
        console.log("accreditorId: ", accreditorId);
    
    res.status(201).json({ });
 };
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!