Last active
January 1, 2016 08:28
-
-
Save sebinsua/8118001 to your computer and use it in GitHub Desktop.
Not able to a catch AssertionError's from within an app wrapped by 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'), | |
sinon = require('sinon'), | |
expect = require('chai').expect; | |
var AssertionError = require("assert").AssertionError; | |
var throwAssertionErrorAndRespond = function (req, res) { | |
throw new AssertionError({ message: "[2] Mocha should see this." }); | |
res.send('{ "success": true }'); | |
}; | |
var obj = { | |
methodContainingSpy: undefined | |
}; | |
describe("a list of failures", function () { | |
var app; | |
beforeEach(function () { | |
app = express(); | |
app.get('/test', throwAssertionErrorAndRespond); | |
app.get('/other-test', function (req, res) { | |
res.send('{ "success": true }'); | |
}); | |
}); | |
it("successfully fails as Mocha can catch AssertionErrors thrown inside the test", function () { | |
throw new AssertionError({ message: "[1] Mocha should see this." }); | |
}); | |
it("successfully fails as Mocha can catch AssertionErrors thrown from a function that was called", function () { | |
throwAssertionErrorAndRespond(); | |
}); | |
it("successfully fails as Mocha can catch AssertionErrors thrown from the end function of supertest", function (done) { | |
request(app).get('/other-test').end(function () { | |
throw new AssertionError({ message: "[3] Mocha should see this." }); | |
done(); | |
}); | |
}); | |
it("Unsuccessfully fails as Mocha is unable to catch the AssertionError thrown by the app function wrapped by supertest", function (done) { | |
request(app).get('/test').end(done); // THIS LOGS "[2] Mocha should see this." but doesn't fail. | |
}); | |
}); | |
describe("using supertest and accessing state afterwards", function () { | |
var app; | |
beforeEach(function () { | |
app = express(); | |
app.get('/spied-request', function (req, res) { | |
if (obj.methodContainingSpy) { | |
obj.methodContainingSpy(); | |
} | |
res.send('{ "success": true }'); | |
}); | |
}); | |
it("spy is successfully accessible after spied-request is accessed from supertest", function (done) { | |
var aSpy = sinon.spy(); | |
obj.methodContainingSpy = function () { | |
aSpy(); | |
}; | |
request(app).get('/spied-request').end(function () { | |
expect(aSpy.calledOnce).to.equal(true); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turned out to be a connect.js feature - something that all web servers should do:
http://stackoverflow.com/questions/20767336/unable-to-catch-exception-from-within-a-supertest-app-request