Stubs are functions that simulate the behavior of another component.
In Sinon, stubs are an extension to spies; this means that all the methods that are available to spies are also available to stubs.
In the context of our tests, we don't really care about the returned value of res.json() – we only care that our checkEmptyPayload middleware function relays this value back faithfully. Therefore, we can turn our res.json spy into a stub, and make it return a reference to an object:
resJsonReturnValue = {};
res = {
status: spy(),
set: spy(),
json: stub().returns(resJsonReturnValue),
};
We can then add another assertion step to compare the value returned by the checkEmptyPayload function, and the value returned by our res.json stub; they should be strictly identical:
describe('and the content-length header is "0"', function () {
let resJsonReturnValue;
let returnedValue;
beforeEach(function () {
...
returnedValue = checkEmptyPayload(req, res, next);
});
...
it('should return whatever res.json() returns', function () {
assert.strictEqual(returnedValue, resJsonReturnValue);
});
...
});
Run the unit tests by executing yarn run test:unit, fix any errors that cause the tests to fail, and then commit the unit tests to the repository:
$ git add -A && git commit -m "Add unit tests for checkEmptyPayload middleware"