Supertest: Log the response body when expectations are not met

Created on 28 Aug 2016  路  3Comments  路  Source: visionmedia/supertest

Hello,

When expectations are not met (example: expected 201 "Created", got 500 "Internal Server Error"), it would be nice to log the response body, as it may contain information for fixing the problem.

Thanks

too-old

Most helpful comment

+1. This has been a pain for me and I've resorted to patching the _assertStatus method, which is undesirable. Something like this:

request.Test.prototype._assertStatus = function (status, res) {
  if (res.status !== status) {
    const expectedStatus = http.STATUS_CODES[status];
    const actualStatus = http.STATUS_CODES[res.status];
    let errorMessage = `expected ${status} "${expectedStatus}", got ${res.status} "${actualStatus}"`;

    // Add a more useful error message.
    if (res.body.error) {
      errorMessage += ` and response error was: \'${res.body.error.message}\'`;
    }

    return new Error(errorMessage);
  }
};

All 3 comments

+1. This has been a pain for me and I've resorted to patching the _assertStatus method, which is undesirable. Something like this:

request.Test.prototype._assertStatus = function (status, res) {
  if (res.status !== status) {
    const expectedStatus = http.STATUS_CODES[status];
    const actualStatus = http.STATUS_CODES[res.status];
    let errorMessage = `expected ${status} "${expectedStatus}", got ${res.status} "${actualStatus}"`;

    // Add a more useful error message.
    if (res.body.error) {
      errorMessage += ` and response error was: \'${res.body.error.message}\'`;
    }

    return new Error(errorMessage);
  }
};

+1 Running into this as well :/

Thanks for the patch @RyanMcDonald - For me it seems to be res.error and not res.body.error, though.

@RyanMcDonald Thanks for that patch. I've created a small module that will un/patch supertest in a similar manner. It preserves the original implementation (making it less likely to break with an update).

See it here: https://gist.github.com/musicin3d/d2d0d63ff7062f40776d27db64ea77ba

Was this page helpful?
0 / 5 - 0 ratings