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

199
Views
Javascript Jest MockImplementation: valores burlones en una función llamada

Estoy tratando de probar los métodos de mi backend (para verificar la cuota de los usuarios, como si pueden usar la aplicación) y tengo algunos problemas con uno específico. De hecho, tengo una función que llama a todos mis métodos y procede a darme los datos que quiero.

Puedo probar diferentes configuraciones con Jest usando mockImplementation en mis métodos.

Pero aquí está mi problema, una de mis funciones llama a otra función que proporciona la cuota del usuario desde mi base de datos:

 export async function getQuota(user_id) { return await Quota.find({ user_id: user_id }).exec();} //return a quota with its data (conso, user, date...) export async function checkIfQuotaIsGood(user_id, date) { let quota; let quotas = await getQuotasByUserId(user_id); //this method can sen error messages if quota received is not good or too small, too old ...

En cada prueba solía burlarme de checkIfQuotaIsGood y decir si es bueno o no:

 ... checkIfQuotaIsGood.mockImplementation(() => Promise.resolve({ conso: 0, user_id: user_id, begin: "1614553200000", end: "1617141600000", quota: 100, }) ); ... await mainFunction(req, res, next); //the function that executes all my methods, it calls checkIfQuotaIsGood which calls getQuota expect(res.status).toHaveBeenCalledWith(200); expect(res.json).toHaveBeenCalledWith({ mainFunction: true,

Mi problema es: en mi función checkIfQuotaIsGood tengo condiciones en la cuota que me envía getQuota. Quiero simular mi función getQuota y dejar que mi función principal llame a checkIfQuotaIsGood, que se ejecutará con los valores simulados. Traté de simular getQuota, pero cuando mi función principal llama a checkIfQuotaIsGood, esta función llamará a getQuota en lugar de usar los valores simulados.

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!