JavaScript Rookie
JavaScript Rookie

Reputation: 253

Argument in mockImplementation() Jest

Does anyone know the args[3] mean in this example?

createTransaction.mockImplementation((...args) => {
   const setResult = args[3]
   setResult({ ...result, approved: false })
})

Upvotes: 0

Views: 131

Answers (1)

Lin Du
Lin Du

Reputation: 102277

...args is called Rest parameters.

while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly

Array index can be used as well.

const mockImplementation = (...args) => console.log(args[3]);

mockImplementation(1,2,3,4) // 4

It will print the fourth element in the args array instance.

Upvotes: 1

Related Questions