11
11
12
12
#### Installation
13
13
14
- This is a addon plugin for the [ Chai Assertion Library] ( http://chaijs.com ) . Install via [ npm] ( http://npmjs.org ) .
14
+ This is an addon plugin for the [ Chai Assertion Library] ( http://chaijs.com ) . Install via [ npm] ( http://npmjs.org ) .
15
15
16
16
npm install chai-http
17
17
@@ -20,8 +20,8 @@ This is a addon plugin for the [Chai Assertion Library](http://chaijs.com). Inst
20
20
Use this plugin as you would all other Chai plugins.
21
21
22
22
``` js
23
- var chai = require (' chai' )
24
- , chaiHttp = require (' chai-http' );
23
+ const chai = require (' chai' );
24
+ const chaiHttp = require (' chai-http' );
25
25
26
26
chai .use (chaiHttp);
27
27
```
@@ -68,13 +68,13 @@ keep the server open, perhaps if you're making multiple requests, you must call
68
68
` .keepOpen() ` after ` .request() ` , and manually close the server down:
69
69
70
70
``` js
71
- var requester = chai .request (app).keepOpen ()
71
+ const requester = chai .request (app).keepOpen ()
72
72
73
73
Promise .all ([
74
74
requester .get (' /a' ),
75
75
requester .get (' /b' ),
76
76
])
77
- .then (responses => ... . )
77
+ .then (responses => { /* ... */ } )
78
78
.then (() => requester .close ())
79
79
```
80
80
@@ -168,7 +168,7 @@ chai.request(app)
168
168
In the following examples we use Chai's Expect assertion library:
169
169
170
170
``` js
171
- var expect = chai . expect ;
171
+ const { expect } = chai;
172
172
```
173
173
174
174
To make the request and assert on its response, the ` end ` method can be used:
@@ -177,7 +177,7 @@ To make the request and assert on its response, the `end` method can be used:
177
177
chai .request (app)
178
178
.put (' /user/me' )
179
179
.send ({ password: ' 123' , confirmPassword: ' 123' })
180
- .end (function (err , res ) {
180
+ .end ((err , res ) => {
181
181
expect (err).to .be .null ;
182
182
expect (res).to .have .status (200 );
183
183
});
@@ -199,16 +199,16 @@ callback has completed, and the assertions can be verified:
199
199
it (' fails, as expected' , function (done ) { // <= Pass in done callback
200
200
chai .request (' http://localhost:8080' )
201
201
.get (' /' )
202
- .end (function (err , res ) {
202
+ .end ((err , res ) => {
203
203
expect (res).to .have .status (123 );
204
204
done (); // <= Call done to signal callback end
205
205
});
206
206
});
207
207
208
- it (' succeeds silently!' , function () { // <= No done callback
208
+ it (' succeeds silently!' , () => { // <= No done callback
209
209
chai .request (' http://localhost:8080' )
210
210
.get (' /' )
211
- .end (function (err , res ) {
211
+ .end ((err , res ) => {
212
212
expect (res).to .have .status (123 ); // <= Test completes before this runs
213
213
});
214
214
});
@@ -227,16 +227,15 @@ and chaining of `then`s becomes possible:
227
227
chai .request (app)
228
228
.put (' /user/me' )
229
229
.send ({ password: ' 123' , confirmPassword: ' 123' })
230
- .then (function (res ) {
230
+ .then ((res ) => {
231
231
expect (res).to .have .status (200 );
232
232
})
233
- .catch (function (err ) {
233
+ .catch ((err ) => {
234
234
throw err;
235
235
});
236
236
```
237
237
238
- __ Note:__ Node.js version 0.10.x and some older web browsers do not have
239
- native promise support. You can use any spec compliant library, such as:
238
+ __ Note:__ Some older web browsers do not have native promise support. You can use any spec compliant library, such as:
240
239
- [ kriskowal/q] ( https://github.com/kriskowal/q )
241
240
- [ stefanpenner/es6-promise] ( https://github.com/stefanpenner/es6-promise )
242
241
- [ petkaantonov/bluebird] ( https://github.com/petkaantonov/bluebird )
@@ -249,7 +248,7 @@ requiring in chai-http. For example:
249
248
if (! global .Promise ) {
250
249
global .Promise = require (' q' );
251
250
}
252
- var chai = require (' chai' );
251
+ const chai = require (' chai' );
253
252
chai .use (require (' chai-http' ));
254
253
```
255
254
@@ -260,16 +259,16 @@ next (for example, when you want to login with the first request, then access an
260
259
261
260
``` js
262
261
// Log in
263
- var agent = chai .request .agent (app)
262
+ const agent = chai .request .agent (app)
264
263
agent
265
264
.post (' /session' )
266
265
.send ({ username: ' me' , password: ' 123' })
267
- .then (function (res ) {
266
+ .then ((res ) => {
268
267
expect (res).to .have .cookie (' sessionid' );
269
268
// The `agent` now has the sessionid cookie saved, and will send it
270
269
// back to the server in the next request:
271
270
return agent .get (' /user/me' )
272
- .then (function (res ) {
271
+ .then ((res ) => {
273
272
expect (res).to .have .status (200 );
274
273
});
275
274
});
0 commit comments