Sinon Mocking

Sinon.js

ComeOnOliver Updated 61 repo stars

File contents

Sinon.js

Spies

const spy = sinon.spy(obj, 'method');
obj.method('arg');
expect(spy.calledWith('arg')).to.be.true;
spy.restore();

Stubs

const stub = sinon.stub(api, 'fetch');
stub.resolves({ id: 1, name: 'Alice' });
stub.withArgs('error').rejects(new Error('fail'));

Fake Timers

const clock = sinon.useFakeTimers();
const debounced = debounce(fn, 100);
debounced(); debounced();
clock.tick(100);
expect(fn.callCount).to.equal(1);
clock.restore();

Sandbox (auto-cleanup)

const sandbox = sinon.createSandbox();
afterEach(() => sandbox.restore());
sandbox.stub(api, 'fetch').resolves([]);

ComeOnOliver/skillshub/tree/main/skills/skillshub-team/catalog-batch5/sinon-mocking commit a8125aa7a4

Frequently asked questions

npx skillmds@latest add comeonoliver/sinon-mocking