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

80
Views
Anule .mockReturnValue en una sola prueba

Estoy tratando de burlarme de la función de un módulo. Quiero que devuelva un valor específico cada vez hasta el punto en que active una función que debería cambiar ese valor a otra cosa. Suponga que no puede tener ninguna expectativa de cuántas veces se activará la función simulada del módulo antes o después de que se active la función de cambio.

Lo que he probado hasta ahora es:

 import * as module from 'module'; jest.mock('module', () => ({ ...jest.requireActual('module'), targetFunction: jest.fn(), })); test('targetFunction returns a different value after changingFunction has fired', async () => { jest .spyOn(module, 'targetFunction') .mockReturnValue('a'); module.start(); // other assertions jest .spyOn(module, 'targetFunction') .mockReturnValue('b'); module.changingFunction(); // other assertions });

Pero sigue regresando después a mi segundo simulacro.

Obviamente, lo anterior es solo una demostración del comportamiento esperado. Antes de que se active changingFunction , quiero que targetFunction devuelva a cada vez que se active. Después de que se activa la función de changingFunction , quiero que targetFunction objetivo devuelva b cada vez que se activa.

Si estoy usando mockReturnValueOnce y sigo encadenándolos, funciona, pero no me gustaría depender de momentos específicos. Quiero cambiar el valor de retorno en un lugar específico del código.

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!