Skip to content

spy.alwaysReturned ​

Returns true, when the fake, spy or stub always returned the provided value.

Uses deep comparison for objects and arrays. Use spy.alwaysReturned(sinon.match.same(obj)) for strict comparison (see matchers).

js
import t from "tap";
import sinon from "sinon";

t.test("spy.alwaysReturned checks if spy always returned value", (t) => {
  const f = sinon.fake.returns("apple pie");

  // Call twice
  f();
  f();

  // Verify alwaysReturned checks
  t.ok(f.alwaysReturned("apple pie"), "should return true for 'apple pie'");
  t.notOk(
    f.alwaysReturned("raspberry pie"),
    "should return false for 'raspberry pie'"
  );

  // Reset and verify
  sinon.reset();
  t.notOk(f.alwaysReturned("apple pie"), "should return false after reset");

  t.end();
});

Resetting alwaysReturned to default ​

You can reset alwaysReturned in three different ways: