From 19b78b7b681a87f1566202bf376054d84ff10cce Mon Sep 17 00:00:00 2001 From: Kevin Mudrick Date: Fri, 2 Sep 2016 16:41:22 -0700 Subject: [PATCH] Fix error parameter returned by mocked superagent to match the documented superagent error handling --- index.js | 8 +++++++- test.js | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 64d53cf..df44168 100644 --- a/index.js +++ b/index.js @@ -89,7 +89,13 @@ function mock(superagent) { try { var response = current(request); if (response.status !== 200) { - cb && cb(response, null); + // superagent puts status and response on the error it returns, + // which should be an actual instance of Error + // See http://visionmedia.github.io/superagent/#error-handling + var error = new Error(response.status); + error.status = response.status; + error.response = response; + cb && cb(error, null); } else { cb && cb(null, response); } diff --git a/test.js b/test.js index cdb4727..4c7f27f 100644 --- a/test.js +++ b/test.js @@ -208,6 +208,8 @@ describe('superagent mock', function() { request.get('/topics/1') .end(function(err, data) { err.should.have.property('status', 500); + err.should.have.property('response'); + should.deepEqual(err.response, {body: {}, status: 500}); done(); }); });