Skip to content

Commit e5fddbb

Browse files
move to ESM (Chai 5) (#310)
* feat: change project to ESM loader Move from CJs to ESModule * move mocha options into package.json * remove requires from test bootstrap * test: fixed tests for chai@5 with new plugin syntax feat: update to latest chai version BREAKING CHANGE: updated typescript interface due to incompatible export changes with esm (cannot export default and object in parallel) * feat: updated non-breaking dependencies fix: move type dependencies to devDependencies * refactor: change var to const declarations * docs: readme.md * fix: make http properties required on type * docs: remove promise note (most clients should support it now) * build: update dependencies * ci: set setup-node to v4 --------- Co-authored-by: Kristján Oddsson <koddsson@gmail.com>
1 parent 62f521c commit e5fddbb

File tree

20 files changed

+779
-13746
lines changed

20 files changed

+779
-13746
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- x.x.x # safety net; don't remove
2222
steps:
2323
- uses: actions/checkout@v4
24-
- uses: actions/setup-node@v3
24+
- uses: actions/setup-node@v4
2525
with:
2626
node-version: ${{ matrix.node_version }}
2727
- run: npm ci

.mocharc.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

.releaserc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"@semantic-release/npm",
77
"@semantic-release/github",
88
["@semantic-release/git", {
9-
"assets": ["dist/*", "package.json", "CHANGELOG.md", "package-lock.json"],
9+
"assets": ["package.json", "CHANGELOG.md", "package-lock.json"],
1010
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
1111
}]
1212
],

README.md

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,13 @@ This is an addon plugin for the [Chai Assertion Library](https://www.chaijs.com/
2020
Use this plugin as you would all other Chai plugins.
2121

2222
```js
23-
const chai = require('chai');
24-
const chaiHttp = require('chai-http');
25-
26-
chai.use(chaiHttp);
27-
```
28-
29-
To use Chai HTTP in an ECMAScript module (ESM)
30-
31-
```js
23+
import chaiModule from "chai";
3224
import chaiHttp from "chai-http";
33-
import * as chaiModule from 'chai';
25+
3426
const chai = chaiModule.use(chaiHttp);
3527
```
3628

37-
To use Chai HTTP on a web page, just include the [`dist/chai-http.js`](dist/chai-http.js) file:
38-
39-
```html
40-
<script src="chai.js"></script>
41-
<script src="chai-http.js"></script>
42-
<script>
43-
chai.use(chaiHttp);
44-
</script>
45-
```
29+
To use Chai HTTP on a web page, please use the latest v4 version for now.
4630

4731
## Integration Testing
4832

@@ -65,7 +49,7 @@ port to listen on for a given test.
6549
__Note:__ This feature is only supported on Node.js, not in web browsers.
6650

6751
```js
68-
chai.request(app)
52+
chai.request.execute(app)
6953
.get('/')
7054
```
7155

@@ -76,7 +60,7 @@ keep the server open, perhaps if you're making multiple requests, you must call
7660
`.keepOpen()` after `.request()`, and manually close the server down:
7761

7862
```js
79-
const requester = chai.request(app).keepOpen()
63+
const requester = chai.request.Request(app).keepOpen()
8064

8165
Promise.all([
8266
requester.get('/a'),
@@ -92,7 +76,7 @@ Promise.all([
9276
You may also use a base url as the foundation of your request.
9377

9478
```js
95-
chai.request('http://localhost:8080')
79+
chai.request.execute('http://localhost:8080')
9680
.get('/')
9781
```
9882

@@ -114,7 +98,7 @@ Examples:
11498
`.set()`
11599
```js
116100
// Set a request header
117-
chai.request(app)
101+
chai.request.execute(app)
118102
.put('/user/me')
119103
.set('Content-Type', 'application/json')
120104
.send({ password: '123', confirmPassword: '123' })
@@ -123,15 +107,15 @@ chai.request(app)
123107
`.send()`
124108
```js
125109
// Send some JSON
126-
chai.request(app)
110+
chai.request.execute(app)
127111
.put('/user/me')
128112
.send({ password: '123', confirmPassword: '123' })
129113
```
130114

131115
`.type()`
132116
```js
133117
// Send some Form Data
134-
chai.request(app)
118+
chai.request.execute(app)
135119
.post('/user/me')
136120
.type('form')
137121
.send({
@@ -144,20 +128,20 @@ chai.request(app)
144128
`.attach()`
145129
```js
146130
// Attach a file
147-
chai.request(app)
131+
chai.request.execute(app)
148132
.post('/user/avatar')
149133
.attach('imageField', fs.readFileSync('avatar.png'), 'avatar.png')
150134
```
151135

152136
`.auth()`
153137
```js
154138
// Authenticate with Basic authentication
155-
chai.request(app)
139+
chai.request.execute(app)
156140
.get('/protected')
157141
.auth('user', 'pass')
158142

159143
// Authenticate with Bearer Token
160-
chai.request(app)
144+
chai.request.execute(app)
161145
.get('/protected')
162146
.auth(accessToken, { type: 'bearer' })
163147

@@ -166,7 +150,7 @@ chai.request(app)
166150
`.query()`
167151
```js
168152
// Chain some GET query parameters
169-
chai.request(app)
153+
chai.request.execute(app)
170154
.get('/search')
171155
.query({name: 'foo', limit: 10}) // /search?name=foo&limit=10
172156
```
@@ -182,7 +166,7 @@ const { expect } = chai;
182166
To make the request and assert on its response, the `end` method can be used:
183167

184168
```js
185-
chai.request(app)
169+
chai.request.execute(app)
186170
.put('/user/me')
187171
.send({ password: '123', confirmPassword: '123' })
188172
.end((err, res) => {
@@ -205,7 +189,7 @@ callback has completed, and the assertions can be verified:
205189

206190
```js
207191
it('fails, as expected', function(done) { // <= Pass in done callback
208-
chai.request('http://localhost:8080')
192+
chai.request.execute('http://localhost:8080')
209193
.get('/')
210194
.end((err, res) => {
211195
expect(res).to.have.status(123);
@@ -214,7 +198,7 @@ it('fails, as expected', function(done) { // <= Pass in done callback
214198
});
215199

216200
it('succeeds silently!', () => { // <= No done callback
217-
chai.request('http://localhost:8080')
201+
chai.request.execute('http://localhost:8080')
218202
.get('/')
219203
.end((err, res) => {
220204
expect(res).to.have.status(123); // <= Test completes before this runs
@@ -232,7 +216,7 @@ If `Promise` is available, `request()` becomes a Promise capable library -
232216
and chaining of `then`s becomes possible:
233217

234218
```js
235-
chai.request(app)
219+
chai.request.execute(app)
236220
.put('/user/me')
237221
.send({ password: '123', confirmPassword: '123' })
238222
.then((res) => {
@@ -243,23 +227,6 @@ chai.request(app)
243227
});
244228
```
245229

246-
__Note:__ Some older web browsers do not have native promise support. You can use any spec compliant library, such as:
247-
- [kriskowal/q](https://github.com/kriskowal/q)
248-
- [stefanpenner/es6-promise](https://github.com/stefanpenner/es6-promise)
249-
- [petkaantonov/bluebird](https://github.com/petkaantonov/bluebird)
250-
- [then/promise](https://github.com/then/promise)
251-
You will need to set the library you use to `global.Promise`, before
252-
requiring in chai-http. For example:
253-
254-
```js
255-
// Add promise support if this does not exist natively.
256-
if (!global.Promise) {
257-
global.Promise = require('q');
258-
}
259-
const chai = require('chai');
260-
chai.use(require('chai-http'));
261-
```
262-
263230
#### Retaining cookies with each request
264231

265232
Sometimes you need to keep cookies from one request, and send them with the

0 commit comments

Comments
 (0)