diff --git a/.gitignore b/.gitignore index 3c3629e6..5eb76826 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +.idea +*.log diff --git a/lib/test.js b/lib/test.js index 7861bd36..55231b6c 100644 --- a/lib/test.js +++ b/lib/test.js @@ -148,6 +148,24 @@ Test.prototype.assert = function(resError, res, fn) { var error; var i; + // check for unexpected network errors or server not running/reachable errors + // when there is no response and superagent sends back a System Error + // do not check further for other asserts, if any, in such case + // https://nodejs.org/api/errors.html#errors_common_system_errors + var sysErrors = { + ECONNREFUSED: 'Connection refused', + ECONNRESET: 'Connection reset by peer', + EPIPE: 'Broken pipe', + ETIMEDOUT: 'Operation timed out' + }; + + if (!res && resError && (resError instanceof Error) && (resError.syscall === 'connect') + && (Object.getOwnPropertyNames(sysErrors).indexOf(resError.code) >= 0)) { + error = new Error(resError.code + ': ' + sysErrors[resError.code]); + fn.call(this, error, null); + return; + } + // asserts for (i = 0; i < this._asserts.length && !error; ++i) { error = this._assertFunction(this._asserts[i], res); diff --git a/test/supertest.js b/test/supertest.js index 159d6a4e..75b03ff2 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -360,6 +360,20 @@ describe('request(app)', function() { }); }); + describe('.expect(status)', function () { + it('should handle connection error', function (done) { + var req = request.agent('http://localhost:1234'); + + req + .get('/') + .expect(200) + .end(function (err, res) { + err.message.should.equal('ECONNREFUSED: Connection refused'); + done(); + }); + }); + }); + describe('.expect(status)', function () { it('should assert only status', function (done) { var app = express();