SuperTest's expect vs. Chai.expect

PositiveGuy picture PositiveGuy · Jul 7, 2015 · Viewed 7.2k times · Source

I'm confused, so if I use SuperTest which apparently looks like it has its own expect assertion, then I don't need to worry about using Chai? Or when I require Chai, Supertest knows about it and is using it as the expect mechanism?

Answer

pmcoltrane picture pmcoltrane · Jul 7, 2015

SuperTest extends SuperAgent's request object to include an expect function. It doesn't work quite like Chai's expect assertion, but can be used to assert the http response status and headers, and can be mixed with Chai's expect.

request(app).
get('/').
expect(200).    // request.expect, is status code 200?
expect('Content-Type', /json/).    // request.expect, does content-type match regex /json/?
expect(function(res){  // request.expect, does this user-provided function throw?
  // user-provided function can include Chai assertions
  expect(res.body).to.exist;
  expect(res.body).to.have.property('status');
}).
end(done);