Feb 1, 2022
Sinon spies are used to record information about function calls.
Unlike mocks or stubs, spies do not replace the function being called.
Spies just record what parameters the function was called with, what value it returned, and other information about the function execution.
const sinon = require('sinon');
const assert = require('assert');
let calls = 0;
let obj = {
myFunction: function(data) {
return ++calls;
}
};
const spy = sinon.spy(obj, 'myFunction');
obj.myFunction('test');
assert.equal(spy.getCall(0).args[0], 'test');
assert.equal(spy.getCall(0).returnValue, 1);
More Stories like this
GitHub – droppyjs/droppy: Self-hosted file storage
Jest VSCode Extension | How to code Tutorial.
Import SVGs as React Components | How to code Tutorial