Skip to content

Instantly share code, notes, and snippets.

@sebinsua
Created December 24, 2013 17:00
Show Gist options
  • Save sebinsua/8115658 to your computer and use it in GitHub Desktop.
Save sebinsua/8115658 to your computer and use it in GitHub Desktop.
Mocha will not detect AssertionError exceptions inside a dependency mocked out using proxyquire when executing a middleware on an express app with supertest.
var express = require('express'),
request = require('supertest'),
expect = require('chai').expect,
proxyquire = require('proxyquire');
var someplaceElsewhere = {
configure: function () {
return {
callsThis: undefined,
aMethod: function (query) {
if (this.callsThis) {
this.callsThis(query);
}
}
};
}
};
var middleware = proxyquire('./fake-middleware', { './someplace-elsewhere': someplaceElsewhere });
describe("Example", function () {
var app;
beforeEach(function () {
middleware.configure();
app = express();
app.configure(function () {
app.use(express.json());
app.use(express.urlencoded()); // note: these two replace: app.use(express.bodyParser());
// see: http://stackoverflow.com/questions/19581146/how-to-get-rid-of-connect-3-0-deprecation-alert
app.use(middleware.handle());
app.use(app.router);
});
app.get('/test', function (req, res) {
res.send('{ "success": true }');
})
});
it("this should be a failure case but for some reason isn't", function (done) {
middleware.middlewareClient.callsThis = function internalAssertions (query) {
expect(query).to.equal("miaow");
};
request(app).get('/test?q=miao').end(done);
});
});
var someplaceElsewhereModule = require('./someplace-elsewhere');
var middleware = {
configure: function () {
this.middlewareClient = someplaceElsewhereModule.configure();
},
handle: function () {
var middlewareScope = this;
return function (req, res, next) {
middlewareScope.middlewareClient.aMethod(req.query.q);
next();
};
}
};
module.exports = middleware;
{
"name" : "not-capturing-assertion-error",
"description" : "Mocha won't catch my assertion error",
"keywords" : [
"assertion",
"error",
"testing",
"middleware",
"mocha"
],
"devDependencies" : {
"express": "~3.4.7",
"supertest": "0.8.2",
"proxyquire": "0.5.2",
"mocha": "*",
"chai": "*"
},
"engines": {
"node": "*"
},
"scripts": {
"test": "mocha"
}
}
var someplaceElsewhere = {
configure: function () {
return {
aMethod: function (query) {
}
};
}
};
module.exports = someplaceElsewhere;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment