Skip to content

stub.yields()

Causes the stub to call the first callback it receives with any provided arguments.

js
import tap from "tap";
import * as sinon from "sinon";

tap.test("stub.yields - basic usage without arguments", (t) => {
  let called = false;

  function bake() {
    called = true;
  }

  const bakeStub = sinon.stub().yields();
  bakeStub(bake);

  t.ok(called, "callback was called");

  t.end();
});

tap.test("stub.yields - with arguments", (t) => {
  let filling;

  function assemble(f) {
    filling = f;
  }

  const assembleStub = sinon.stub().yields("raspberry");
  assembleStub(assemble);

  t.equal(filling, "raspberry", "callback called with raspberry");

  t.end();
});

If a method accepts more than one callback, you need to use yieldsRight to call the last callback or callsArg to have the stub invoke other callbacks than the first or last one.

See also