I ran into an issue today where I had passing tests that should have failed. There were some incorrectly stubbed methods using Sinon that failed to stub out the callback correctly. I would have expected execution to fail, but it passed the test, however it stopped execution early because my test messages quit logging to the console at the incorrectly stubbed method invocation.
The code I had was similar to the following:
exports.myFunc = function (req, res) {
var innerFunc = function(callback) {
fs.exists('some/dir', function (exists) {
if (!exists) { // handle error }
else {
logger.log('this prints to the console');
utilModule.stubbedMethod('some/dir', callback);
}
});
};
logger.log('this also prints to the console');
innerFunc(function (err, value) {
logger.log('this does not print to the console');
if (err) {
logger.log('this does not print to the console');
res.status(500).send(err);
} else {
logger.log('this does not print to the console, either');
res.send('success');
}
});
});
// This is the tests, now:
var app = require('../app'),
mod = require('../lib/myModule'),
request = require('supertest'),
chai = require('chai'),
sinon = require('sinon');
describe('For module myModule', function () {
...
describe('#myFunc', function () {
var fs = require('fs'),
util = require('../lib/utilModule');
describe('Happy path', function () {
var expectedResult = "expected";
before(function() {
sinon.stub(fs, 'exists').yields(true, null);
sinon.stub(mod, 'stubbedMethod').yield("some value");
// the above is wrong and should have been yield(null, null, "some value")
});
after(function () { fs.exists.restore(); mod.stubbedMethod.restore(); });
it('Replies with the right value and status 200', function () {
request(app).get('/url').expect(200, 'success').end(function(err, res) {
if (err) throw err;
}
});
});
});
Why did this test pass, and is there some way to prevent it from passing?