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

59
Views
jest mockResolvedValueOnce n times

I have code that (greatly simplified and with pseudocode) looks like this:

obj1 = [1,2,3],
obj2 = "foo"

returned_values = []

for item in obj1:
    returned_values.push(connection.query(item))
returned_values.push(connection.query(obj2)

The calls of item and obj2 should return different values from the mocked db.

We could mock it like this:

jest.spyOn(connection, "query")
    .mockResolvedValueOnce("foo")
    .mockResolvedValueOnce("foo")
    .mockResolvedValueOnce("foo")
    .mockResolvedValueOnce("bar")

and call it a day.

Problem is: I have different test cases with obj1 having different lengths - it can be an empty array, it can have 1 element, or 10. I know how many it has before runtime.

Is there something like "mockResolvedValueNTimes(times, value)" or an approach that lets me mock the same return value n times?

I cant flip the order in the real code in which the DB calls are made. The last call depends on the calls b4

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!