Supertest: How can I get response when expect(statusCode) fail in catch function?

Created on 15 Sep 2017  路  4Comments  路  Source: visionmedia/supertest

Hello.

I want to show detail error message with body response when request has filed.

it('show detail error when status expect fail', () => {
request(server)
   .get(ruiPath)
   .expect(200)
   .then(() => { // some expect } )
   .then(done)
   .catch((err) => {
      // I wanna get response here to display error message more kindly with response body sent from server.
     console.error(err.response.body);
     done.fail(err);
   });
}
question

All 4 comments

use .end((err, res) => { after the .expect(200)

The same question.
I know the .end(fn) can got the response.
But have to change too much code for that.Because it can't .then after .end.
MayBe supertest can inject the response to the error object.

I think this should be re-opened. Using the promises interface, or async/await, I'd expect to be able to do this:

return request
    .get(url)
    .expect(200)
    .catch(e => {
        console.log(e, e.response, e.status);
    });

...just as in superagent, for example: https://github.com/visionmedia/superagent/issues/1074. I don't want to use .end callbacks. Is this something that could be achieved?

Looks like you need to proxy Test.prototype.then in the same way as Test.prototype.end (see https://github.com/visionmedia/superagent/blob/1724997edc451df62139385266d17438002d918f/lib/request-base.js#L231).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hacker0limbo picture hacker0limbo  路  3Comments

mayacr86 picture mayacr86  路  4Comments

DeaconDesperado picture DeaconDesperado  路  6Comments

nitrocode picture nitrocode  路  4Comments

jonathanong picture jonathanong  路  4Comments