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

159
Views
¿Manejador de ruta de prueba unitaria con Jest?

Tengo el siguiente fragmento de código que funciona como controlador para mi ruta determinada. Estoy tratando de realizar la prueba unitaria usando Jest pero no tengo idea de qué cosas puedo probar en la siguiente función. Logré probar algo, pero no estoy seguro de si estoy en la dirección correcta o no y qué probar.

ruta.js

 exports.routeHandler = function (param1, param2) { return async function(req, res){ const { id } = req.params; try { const user = await param1.user.getAuthUser(res); if(!user) return res.sendStatus(404).json(); const results = await getUserId(user, param1, param2); const getInformation = await getInfo(results, id, param1, param2); res.sendStatus(200).json({ getInformation }) } catch (error) { return res.sendStatus(500); } } }

ruta.prueba.js

 const mockResponse = () => { const res = {}; res.sendStatus = jest.fn().mockReturnValue(res); res.json = jest.fn().mockReturnValue(res); return res; } const mockRequest = () => { return { params: { id : 1 } } } const mockParam1 = { user: { getAuthUser: jest.fn() } }; const mockParam2 = { error: jest.fn() }; describe('checkAuth', () => { test('should 404 if user is not set', async() => { const req = mockRequest(); const res = mockResponse(); const routeHandlerTest = routeHandler(mockParam1, mockParam2); await routeHandlerTest(req, res) expect(res.sendStatus).toHaveBeenCalledWith(404); }) })

Otro problema aquí es que no puedo simular o espiar la función getUserId y getInfo .

getUserId.js

 async function getUserId(user, param1, param2) { try { return await param1.database.query( 'SELECT "id" from "user" WHERE "username" = $username', { type: "SELECT", bind: { username: user.username }, } ); } catch (error) { param2.error(error); } }

getInfo.js

 async function getInfo(results, id, param1, param2) { try { return await param1.database.query( // query , { type: "SELECT", bind: { id: id, userId: results[0].id, }, } ); } catch (error) { param2.error(error); } }

Nota: las funciones getUserId y getInfo están en módulos diferentes

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!