Supertest: Supertest should report custom error messages

Created on 21 Jan 2014  路  10Comments  路  Source: visionmedia/supertest

At the moment when the status message does not match the expectations we get an error message like:

"expected 200 "OK", got 500 "Internal server error"

It would be helpful to display a server custom error message if any, as defined by:

res.send(500, { error: 'something blew up' });

would display:

"expected 200 "OK", got 500 "something blew up"

Most helpful comment

I'm feeling the same way. Its really non-descriptive to have the standard status code error. However this is a pretty simple work around when trying to the match status code:

request(app)
.get(path)
.expect(function (res) {
  var expectedCode = 302
  var result = res.statusCode + " " + JSON.stringify(res.text)
  var expected = expectedCode + " " + JSON.stringify(res.text)
  assert.equal(result, expected)
})

All 10 comments

meh, .error is pretty arbitrary it could be any property, .error.message etc, there's no standard, plus if all you send is .error it might as well be just a text/plain response

:+1: Please reopen

I think this should be a part of supertest. Fixing a bug in the code, where the test simply states the error was 500 Internal Server Error is difficult. And it's extra frustrating if you know your code is actually returning a meaningful error message.

Others seem to feel the same way: https://github.com/frenchie4111/supertest/commit/850d5d893d313f4515a581c16cb173a74fd7b4f8 (supertest fork)

+1

Reasoning: 500 Internal Server Error is a pretty generic error used for so many things, when it's received, troubleshooting is typically difficult. Custom error messages would make the troubleshooting process easier.

This problem is magnified if you happen to work in a project that's big enough that no one developer knows all of the code.

+1, but maybe we should just add the contents of res.body. It should contain the relevant error messages that would help devs figure out what the errors are.

An alternative would be to pass a function that can be called when the expect fails, and pass the response object so the dev can extract the relevant information.

+1 for:

but maybe we should just add the contents of res.body. It should contain the relevant error messages that would help devs figure out what the errors are.

I'm feeling the same way. Its really non-descriptive to have the standard status code error. However this is a pretty simple work around when trying to the match status code:

request(app)
.get(path)
.expect(function (res) {
  var expectedCode = 302
  var result = res.statusCode + " " + JSON.stringify(res.text)
  var expected = expectedCode + " " + JSON.stringify(res.text)
  assert.equal(result, expected)
})

While that workaround is simple, repeating it for every test is quite painful. I could define function like this:

function ok(res) {
    if (res.status !== 200) {
        var b = http.STATUS_CODES[res.status];
        return new Error('expected 200, got ' + res.status + ' "' + b + '" with message: ' + res.text);
    }
}

and then use

.expect(ok)

But it is still a workaround - having a way to specify error-reporting logic for a supertest instance would be much better.

@tj 4xx error messages are not arbitrary. They are set with the http response like with any other status code. In Express, for example you can do

res.statusMessage = "whatever yuou want"

Any http client will interpret that as the status code message.

In my tests I can access this with res.res.statusMessage. This looks like the Express response object to me, but when I use the browser it interprets the response as "400 whatever you want"

Here's an updated version pull request to log the response to stderr in case anyone needs it: https://github.com/frenchie4111/supertest/pull/1

Wow, can't believe this basic debugging functionality is not in supertest. This is making our life a lot harder than it has to be

Was this page helpful?
0 / 5 - 0 ratings