jestjsdynamoose

Using original function inside jest mock causes infinite loop


I have a chainable method from dynamoose where in testing I would like to add an additional method to the chain.

Essentially, in testing I would like to replace all instances of

Model.query(key).otherChainableMethods()

with

Model.query(key).limit(LIMIT).otherChainableMethods()

So I tried:

Model.query = jest.fn(key => Model.query(key).limit(LIMIT))

but this is clearly causing some sort of infinite loop, because I get RangeError: Maximum call stack size exceeded

How can use the original implementation of a function inside the mock implementation?


Solution

  • I found a simple solution. I can simply store the original method in another variable first.

    const originalQuery = Profile.query
    Profile.query = jest.fn(key => originalQuery(key).limit(LIMIT))