How do I stub a chain of methods in Sinon?

IMHO, we can just use returns to do this. We don't need to use callsFake or mock it as function.

// Cars.find().fetch()

sinon.stub(Cars, 'find').returns({
  fetch: sinon.stub().returns(anything)
});

in case, if there is another method after fetch(), we can use returnsThis()

// Cars.find().fetch().where()

sinon.stub(Cars, 'find').returns({
  fetch: sinon.stub().returnsThis(),
  where: sinon.stub().returns(anything)
});

Ref: https://sinonjs.org/releases/v6.3.3/

Hope it helps


Try this:

sandbox.stub(Cars, "find", () => {
    return {
        fetch: sinon.stub().returns(anything);
    };
});