Skip to content

Getting started ​

The examples will be using Node-Tap, which is the simplest complete test runner that is known to Sinon maintainers.

You can use Sinon with most popular test runners, such as Mocha, Jasmine, Jest, etc.

Installing ​

sh
npm install -D sinon

Using ​

js
// import the test framework
import tap from "tap";

// import sinon
import * as sinon from "sinon";

tap.test("this is a test", (t) => {
  // create a fake
  const fake = sinon.fake();

  // call the fake
  fake();

  // assert on the fake
  t.ok(fake.calledOnce);

  t.end();
});

Cleaning up after tests ​

In order to avoid memory leaks, which could lead to unpredictable test failures, it is recommended to restore Sinon's default sandbox after each test.

js
t.afterEach((t) => {
  sinon.restore();
});

For more advanced setups with sandboxes, please see the sandbox section.