DEV Community

Cover image for Mocking with Sinon.js: A Comprehensive Guide
Adesoji Daniel
Adesoji Daniel

Posted on

Mocking with Sinon.js: A Comprehensive Guide

Testing is an integral part of software development, ensuring that code behaves as expected. When it comes to JavaScript, Sinon.js is a powerful library for creating spies, stubs, and mocks, making it easier to test code that relies on external dependencies. This article will explore how to use Sinon.js to mock tests effectively.

Introduction to Sinon.js

Sinon.js is a standalone test double library for JavaScript. It works with any testing framework and allows you to create spies, stubs, and mocks to verify the behavior of your code.

  • Spies: Track calls to functions and record information such as call arguments, return values, and the value of this.

  • Stubs: Replace functions with custom implementations and control their behavior.

  • Mocks: Simulate and verify the behavior of entire objects.

Getting Started

Before you start, ensure you have Node.js and npm installed. You can install Sinon.js using npm:

npm install sinon --save-dev
Enter fullscreen mode Exit fullscreen mode

Basic Usage

Let's start with some basic examples of how to use Sinon.js.

Creating Spies

A spy is a function that records arguments, return values, and the value of this for all its calls.

const sinon = require('sinon');

function greet(name) {
  console.log(`Hello, ${name}!`);
}

const spy = sinon.spy(greet);
spy('John');

console.log(spy.called); // true
console.log(spy.calledWith('John')); // true
Enter fullscreen mode Exit fullscreen mode
Using Stubs

Stubs allow you to replace a function with a custom implementation. This is useful for isolating the function under test from its dependencies.

const sinon = require('sinon');

const userService = {
  getUser: function (id) {
    // Simulate a database call
    return { id, name: 'John Doe' };
  }
};

const stub = sinon.stub(userService, 'getUser').returns({ id: 1, name: 'Jane Doe' });

const user = userService.getUser(1);
console.log(user); // { id: 1, name: 'Jane Doe' }

stub.restore();
Enter fullscreen mode Exit fullscreen mode
Creating Mocks

Mocks combine the functionality of spies and stubs, allowing you to both replace methods and assert that they are called correctly.

const sinon = require('sinon');

const userService = {
  getUser: function (id) {
    // Simulate a database call
    return { id, name: 'John Doe' };
  }
};

const mock = sinon.mock(userService);
mock.expects('getUser').once().withArgs(1).returns({ id: 1, name: 'Jane Doe' });

const user = userService.getUser(1);
console.log(user); // { id: 1, name: 'Jane Doe' }

mock.verify();
mock.restore();
Enter fullscreen mode Exit fullscreen mode

Advanced Usage

Now that we've covered the basics, let's dive into more advanced scenarios.

Asynchronous Stubs

To test asynchronous functions, you can use Sinon.js stubs to simulate async behavior.

const sinon = require('sinon');

const fetchData = function (callback) {
  setTimeout(() => {
    callback('data');
  }, 1000);
};

const callback = sinon.fake();
sinon.stub(global, 'setTimeout').callsFake((fn, timeout) => fn());

fetchData(callback);

console.log(callback.calledWith('data')); // true

global.setTimeout.restore();
Enter fullscreen mode Exit fullscreen mode
Stubbing Dependencies in Modules

When testing a module, you may need to stub its dependencies. This can be done using Sinon.js with a module bundler like Webpack or tools like proxyquire.

const sinon = require('sinon');
const proxyquire = require('proxyquire');

const dbStub = {
  query: sinon.stub().returns(Promise.resolve(['result1', 'result2']))
};

const myModule = proxyquire('./myModule', { './db': dbStub });

myModule.getData().then(data => {
  console.log(data); // ['result1', 'result2']
});
Enter fullscreen mode Exit fullscreen mode
Verifying Call Order

Sinon.js allows you to verify the order in which functions are called.

const sinon = require('sinon');

const first = sinon.spy();
const second = sinon.spy();

first();
second();

sinon.assert.callOrder(first, second); // Passes
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Isolate Tests: Use stubs and mocks to isolate the function under test from its dependencies.

  • Restore Original Functions: Always restore original functions after stubbing or mocking to avoid side effects.

  • Use Fakes for Complex Behavior: Use sinon.fake to create complex behavior for functions that require intricate setups.

Conclusion

Sinon.js is a versatile tool for creating spies, stubs, and mocks in JavaScript. It seamlessly integrates with any testing framework, making it easier to write robust and maintainable tests. By following the examples and best practices outlined in this article, you can effectively mock tests and ensure the reliability of your code. Happy testing!

Top comments (0)