Skip to content

stub.callsArgOnAsync(index, object)

Causes the stub to call the argument at the provided index as a callback function, with an additional object parameter to pass the this context, asynchronously.

js
import * as sinon from "sinon";
const car = {
  color: "red"
};
const stub = sinon.stub().callsArgOnAsync(0, car);

function updateColor() {
  this.color = "blue";
}

console.log(car.color);
// => red

stub(updateColor);
console.log(car.color);
// => red

setTimeout(function () {
  console.log(car.color);
  // => blue
}, 1);

Errors

When the argument at the provided index is undefined, or not a function, an Error will be thrown.

js
import * as sinon from "sinon";
const car = {
  color: "red"
};
const stub = sinon.stub().callsArgOnAsync(0, car);

const pie = "apple pie";

stub(pie);
// => Uncaught TypeError: argument at index 0 is not a function: apple pie

stub(nonExistingCallback);
// => Uncaught ReferenceError: nonExistingCallback is not defined

See also

More information