Created
December 24, 2013 17:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
See: http://stackoverflow.com/questions/20767336/unable-to-catch-exception-from-within-a-supertest-app-request