Search code examples
node.jsmocha.jssupertest

Is it possible to add information to the error message in supertest


I'm utilizing supertest with mocha to test a nodejs express app. Everything is fine expect I'd like a little more descriptive error messages. That's not to say the messages are currently bad, they're not. I'd just like a little more information.

For example:

it('should successfully post my data and return a valid JSON', function(done) {
  .post(url)
  .set('Accept', 'application/json')
  .expect('Content-Type', /json/)
  .send(data)
  .expect(201, resultBody)
  .end(function(err, res) {
    if (err) return done(err);
    done();
  });
});

If an error should occur, such that the resultBody doesn't match the actual result, it'll print out a nice + expected - actual message. But I would also like to see other information (maybe the authentication or the header).

My current solution is to do the following:

  .end(function(err, res) {
    if (err) {
      console.log(res.headers);
      console.log(res.statusCode);
      console.log(res.body);
      return done(err);
    }
    done();
  });

But the console.log messages appear inline with the "it" messages, before the passing/pending/failing messages and not with the actual error.

Is it possible, without too much extra effort, to put extra information with the error message?


Solution

  • You can fix it using the Language Chains of Chai in expect and should adding Assertion Styles:

    var foo = false;
    expect(foo, 'foo must be true').to.be.true;
    //AssertionError: foo must be true: expected false to be true