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?
Asked
Active
Viewed 8,373 times
9
-
I'm confused as to what the question is here? Do you want to understand the inner workings of the test frameworks and assertion libraries? – Clarkie Jul 07 '15 at 19:38
-
When you use SuperTest to make a request and you start to assert using 'expect' I assume since I've defined expect via Chai assertion library that that is the library it's using. In other words SuperTest doesn't have its own assertion library right? You have to tell it what library by setting up the expect variable like you do in Mocha and then SuperTest is able to take an use that variable somehow an append it to the end of it's request function call – PositiveGuy Jul 07 '15 at 19:40
-
hmm just looked at SuperTest's docs again and it says it's using SuperAgent's 'expect' function it looks like because it says 'HTTP assertions made easy via super-agent.' But then it also says 'SuperTest works with any test framework, here is an example without using any test framework at all:' so I'm confused here. – PositiveGuy Jul 07 '15 at 19:41
-
I think I got it...it's superagent that provides an expect() function but until you specify which assertion library that expect() should use, it won't work and so since I set expect = require('chai).expect superagent now uses Chai for its expect method – PositiveGuy Jul 07 '15 at 19:48
1 Answers
18
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);

pmcoltrane
- 3,052
- 1
- 24
- 30
-
thx. Just wondering what situation would you wanna in a BDD test assert using SuperTest's 'expect' and then right after use a Chai 'expect'. I mean once you expect something and it is true, your BDD test passes...I just don't see a need for Chai expect then when testing integration request/responses in your BDD or other integration tests... – PositiveGuy Jul 07 '15 at 20:04