@@ -127,6 +127,41 @@ chai.request(app)
127
127
});
128
128
```
129
129
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
+
130
165
#### Dealing with the response - Promises
131
166
132
167
If ` Promise ` is available, ` request() ` becomes a Promise capable library -
0 commit comments