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