Skip to content

Commit fac5372

Browse files
committedMar 11, 2016
Add asyncronous caveat documentation
1 parent c158306 commit fac5372

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
 

‎README.md

+35
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,41 @@ chai.request(app)
127127
});
128128
```
129129

130+
##### Caveat
131+
132+
Because the `end` function is passed a callback, assertions are run
133+
asynchronously. Therefore, a mechanism must be used to notify the testing
134+
framework that the callback has completed. Otherwise, the test will pass before
135+
the assertions are checked.
136+
137+
For example, in the [Mocha test framework](http://mochajs.org/), this is
138+
accomplished using the
139+
[`done` callback](https://mochajs.org/#asynchronous-code), which signal that the
140+
callback has completed, and the assertions can be verified:
141+
142+
```js
143+
it('fails, as expected', function(done) { // <= Pass in done callback
144+
chai.request('http://localhost:8080')
145+
.get('/')
146+
.end(function(err, res) {
147+
expect(res).to.have.status(123);
148+
done(); // <= Call done to signal callback end
149+
});
150+
}) ;
151+
152+
it('succeeds silently!', function() { // <= No done callback
153+
chai.request('http://localhost:8080')
154+
.get('/')
155+
.end(function(err, res) {
156+
expect(res).to.have.status(123); // <= Test completes before this runs
157+
});
158+
}) ;
159+
```
160+
161+
When `done` is passed in, Mocha will wait until the call to `done()`, or until
162+
the [timeout](http://mochajs.org/#timeouts) expires. `done` also accepts an
163+
error parameter when signaling completion.
164+
130165
#### Dealing with the response - Promises
131166

132167
If `Promise` is available, `request()` becomes a Promise capable library -

0 commit comments

Comments
 (0)
Please sign in to comment.