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

203
Vistas
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
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