Skip to content

Commit ee4952e

Browse files
committedFeb 26, 2021
docs: update README examples to modern syntax
1 parent 233118b commit ee4952e

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed
 

‎README.md

+17-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#### Installation
1313

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).
1515

1616
npm install chai-http
1717

@@ -20,8 +20,8 @@ This is a addon plugin for the [Chai Assertion Library](http://chaijs.com). Inst
2020
Use this plugin as you would all other Chai plugins.
2121

2222
```js
23-
var chai = require('chai')
24-
, chaiHttp = require('chai-http');
23+
const chai = require('chai');
24+
const chaiHttp = require('chai-http');
2525

2626
chai.use(chaiHttp);
2727
```
@@ -68,13 +68,13 @@ keep the server open, perhaps if you're making multiple requests, you must call
6868
`.keepOpen()` after `.request()`, and manually close the server down:
6969

7070
```js
71-
var requester = chai.request(app).keepOpen()
71+
const requester = chai.request(app).keepOpen()
7272

7373
Promise.all([
7474
requester.get('/a'),
7575
requester.get('/b'),
7676
])
77-
.then(responses => ....)
77+
.then(responses => { /* ... */ })
7878
.then(() => requester.close())
7979
```
8080

@@ -168,7 +168,7 @@ chai.request(app)
168168
In the following examples we use Chai's Expect assertion library:
169169

170170
```js
171-
var expect = chai.expect;
171+
const { expect } = chai;
172172
```
173173

174174
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:
177177
chai.request(app)
178178
.put('/user/me')
179179
.send({ password: '123', confirmPassword: '123' })
180-
.end(function (err, res) {
180+
.end((err, res) => {
181181
expect(err).to.be.null;
182182
expect(res).to.have.status(200);
183183
});
@@ -199,16 +199,16 @@ callback has completed, and the assertions can be verified:
199199
it('fails, as expected', function(done) { // <= Pass in done callback
200200
chai.request('http://localhost:8080')
201201
.get('/')
202-
.end(function(err, res) {
202+
.end((err, res) => {
203203
expect(res).to.have.status(123);
204204
done(); // <= Call done to signal callback end
205205
});
206206
});
207207

208-
it('succeeds silently!', function() { // <= No done callback
208+
it('succeeds silently!', () => { // <= No done callback
209209
chai.request('http://localhost:8080')
210210
.get('/')
211-
.end(function(err, res) {
211+
.end((err, res) => {
212212
expect(res).to.have.status(123); // <= Test completes before this runs
213213
});
214214
});
@@ -227,16 +227,15 @@ and chaining of `then`s becomes possible:
227227
chai.request(app)
228228
.put('/user/me')
229229
.send({ password: '123', confirmPassword: '123' })
230-
.then(function (res) {
230+
.then((res) => {
231231
expect(res).to.have.status(200);
232232
})
233-
.catch(function (err) {
233+
.catch((err) => {
234234
throw err;
235235
});
236236
```
237237

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:
240239
- [kriskowal/q](https://github.com/kriskowal/q)
241240
- [stefanpenner/es6-promise](https://github.com/stefanpenner/es6-promise)
242241
- [petkaantonov/bluebird](https://github.com/petkaantonov/bluebird)
@@ -249,7 +248,7 @@ requiring in chai-http. For example:
249248
if (!global.Promise) {
250249
global.Promise = require('q');
251250
}
252-
var chai = require('chai');
251+
const chai = require('chai');
253252
chai.use(require('chai-http'));
254253
```
255254

@@ -260,16 +259,16 @@ next (for example, when you want to login with the first request, then access an
260259

261260
```js
262261
// Log in
263-
var agent = chai.request.agent(app)
262+
const agent = chai.request.agent(app)
264263
agent
265264
.post('/session')
266265
.send({ username: 'me', password: '123' })
267-
.then(function (res) {
266+
.then((res) => {
268267
expect(res).to.have.cookie('sessionid');
269268
// The `agent` now has the sessionid cookie saved, and will send it
270269
// back to the server in the next request:
271270
return agent.get('/user/me')
272-
.then(function (res) {
271+
.then((res) => {
273272
expect(res).to.have.status(200);
274273
});
275274
});

0 commit comments

Comments
 (0)
Please sign in to comment.