Introduction to Sinon
Sinon is a popular JavaScript library widely used for creating spies, stubs, mocks, and fake servers in unit testing. Its APIs work seamlessly with testing frameworks like Mocha, Jasmine, and QUnit, enabling software developers to test their code efficiently.
Examples of Sinon APIs
Creating a Spy
A spy wraps a function and provides information about its calls. Here’s a quick example:
const sinon = require('sinon'); let myObject = { myMethod: function() {} }; let spy = sinon.spy(myObject, 'myMethod'); myObject.myMethod(); console.log(spy.called); // true
Creating a Stub
Stubs are like spies but also allow you to set predefined behavior for the functions being stubbed. Example:
let stub = sinon.stub(myObject, 'myMethod').returns(42); console.log(myObject.myMethod()); // 42
Using Mocks
Mocks are similar to spies and stubs, but they also set expectations and verify that the function behaves as expected:
let mock = sinon.mock(myObject); mock.expects('myMethod').once().withArgs(1, 2, 3); myObject.myMethod(1, 2, 3); mock.verify();
Fake Timers
Sinon provides fake timers to control the flow of time in tests, which is useful for testing time-dependent code:
let clock = sinon.useFakeTimers(); setTimeout(() => console.log('Time flies!'), 1000); clock.tick(1000); // Output: 'Time flies!'
Fake XMLHttpRequest (XHR)
Simulating server responses with Fake XHR is made easy with Sinon:
let xhr = sinon.useFakeXMLHttpRequest(); xhr.onCreate = function (req) { setTimeout(() => { req.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ message: 'Success' })); }, 1000); }; let request = new XMLHttpRequest(); request.onload = function() { console.log(request.responseText); // 'Success' }; request.open('GET', '/some-url', true); request.send();
App Example Using Sinon
Below is an example of how you can use Sinon to test an application’s functionality:
// Assume we have a User class with a method to load user data from a server class User { load(callback) { var xhr = new XMLHttpRequest(); xhr.open('GET', '/user/12345', true); xhr.onload = function() { if (xhr.status === 200) { callback(null, JSON.parse(xhr.responseText)); } else { callback(new Error('Error loading user')); } }; xhr.send(); } } // Test using Sinon let user = new User(); let callback = sinon.fake(); sinon.replace(XMLHttpRequest.prototype, 'send', function() { setTimeout(() => { this.onload({ target: { status: 200, responseText: JSON.stringify({ id: 12345, name: 'John Doe' }) } }); }, 100); }); user.load(callback); console.log(callback.calledOnce); // true console.log(callback.firstCall.args[1].name); // 'John Doe'
With these utilities, Sinon makes it easier to write comprehensive tests, ensuring your JavaScript code works as intended under various conditions.
Hash: 0a9d2aed8af6f9201d14f7be3ff8f59f76fa0c6f98d86a2eff8b6bc56936a47b