Skip to content

Commit 1b744e7

Browse files
committed
Merge branch 'main' into pq
2 parents c0b98d9 + 28f4426 commit 1b744e7

File tree

5 files changed

+72
-38
lines changed

5 files changed

+72
-38
lines changed

README.md

+59-24
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ 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-
import chaiModule from "chai";
23+
import * as chai from "chai";
2424
import chaiHttp from "chai-http";
2525

26-
const chai = chaiModule.use(chaiHttp);
26+
chai.use(chaiHttp);
27+
28+
// if you need to access `request`
29+
import {default as chaiHttp, request} from "chai-http";
30+
chai.use(chaiHttp);
31+
32+
request.get(...).send(...);
33+
34+
// or setting up an app
35+
request.execute(app);
2736
```
2837

2938
To use Chai HTTP on a web page, please use the latest v4 version for now.
@@ -49,18 +58,22 @@ port to listen on for a given test.
4958
__Note:__ This feature is only supported on Node.js, not in web browsers.
5059

5160
```js
52-
chai.request.execute(app)
61+
import {request} from 'chai-http';
62+
63+
request.execute(app)
5364
.get('/')
5465
```
5566

56-
When passing an `app` to `request`; it will automatically open the server for
67+
When passing an `app` to `request.execute()`, it will automatically open the server for
5768
incoming requests (by calling `listen()`) and, once a request has been made
5869
the server will automatically shut down (by calling `.close()`). If you want to
5970
keep the server open, perhaps if you're making multiple requests, you must call
6071
`.keepOpen()` after `.request()`, and manually close the server down:
6172

6273
```js
63-
const requester = chai.request.Request(app).keepOpen()
74+
import {request} from 'chai-http';
75+
76+
const requester = request.Request(app).keepOpen()
6477

6578
Promise.all([
6679
requester.get('/a'),
@@ -76,7 +89,9 @@ Promise.all([
7689
You may also use a base url as the foundation of your request.
7790

7891
```js
79-
chai.request.execute('http://localhost:8080')
92+
import {request} from 'chai-http';
93+
94+
request.execute('http://localhost:8080')
8095
.get('/')
8196
```
8297

@@ -89,33 +104,39 @@ Once a request is created with a given VERB (get, post, etc), you chain on these
89104
| `.set(key, value)` | Set request headers |
90105
| `.send(data)` | Set request data (default type is JSON) |
91106
| `.type(dataType)` | Change the type of the data sent from the `.send()` method (xml, form, etc) |
92-
| `.attach(field, file, attachment)` | Attach a file |
107+
| `.attach(field, file, attachment)` | Attach a file |
93108
| `.auth(username, password)` | Add auth headers for Basic Authentication |
94109
| `.query(parmasObject)` | Chain on some GET parameters |
95110

96111
Examples:
97112

98113
`.set()`
99114
```js
115+
import {request} from 'chai-http';
116+
100117
// Set a request header
101-
chai.request.execute(app)
118+
request.execute(app)
102119
.put('/user/me')
103120
.set('Content-Type', 'application/json')
104121
.send({ password: '123', confirmPassword: '123' })
105122
```
106123

107124
`.send()`
108125
```js
126+
import {request} from 'chai-http';
127+
109128
// Send some JSON
110-
chai.request.execute(app)
129+
request.execute(app)
111130
.put('/user/me')
112131
.send({ password: '123', confirmPassword: '123' })
113132
```
114133

115134
`.type()`
116135
```js
136+
import {request} from 'chai-http';
137+
117138
// Send some Form Data
118-
chai.request.execute(app)
139+
request.execute(app)
119140
.post('/user/me')
120141
.type('form')
121142
.send({
@@ -127,30 +148,36 @@ chai.request.execute(app)
127148

128149
`.attach()`
129150
```js
151+
import {request} from 'chai-http';
152+
130153
// Attach a file
131-
chai.request.execute(app)
154+
request.execute(app)
132155
.post('/user/avatar')
133156
.attach('imageField', fs.readFileSync('avatar.png'), 'avatar.png')
134157
```
135158

136159
`.auth()`
137160
```js
161+
import {request} from 'chai-http';
162+
138163
// Authenticate with Basic authentication
139-
chai.request.execute(app)
164+
request.execute(app)
140165
.get('/protected')
141166
.auth('user', 'pass')
142-
167+
143168
// Authenticate with Bearer Token
144-
chai.request.execute(app)
169+
request.execute(app)
145170
.get('/protected')
146-
.auth(accessToken, { type: 'bearer' })
147-
171+
.auth(accessToken, { type: 'bearer' })
172+
148173
```
149174

150175
`.query()`
151176
```js
177+
import {request} from 'chai-http';
178+
152179
// Chain some GET query parameters
153-
chai.request.execute(app)
180+
request.execute(app)
154181
.get('/search')
155182
.query({name: 'foo', limit: 10}) // /search?name=foo&limit=10
156183
```
@@ -166,7 +193,9 @@ const { expect } = chai;
166193
To make the request and assert on its response, the `end` method can be used:
167194

168195
```js
169-
chai.request.execute(app)
196+
import {request} from 'chai-http';
197+
198+
request.execute(app)
170199
.put('/user/me')
171200
.send({ password: '123', confirmPassword: '123' })
172201
.end((err, res) => {
@@ -188,8 +217,10 @@ accomplished using the
188217
callback has completed, and the assertions can be verified:
189218

190219
```js
220+
import {request} from 'chai-http';
221+
191222
it('fails, as expected', function(done) { // <= Pass in done callback
192-
chai.request.execute('http://localhost:8080')
223+
request.execute('http://localhost:8080')
193224
.get('/')
194225
.end((err, res) => {
195226
expect(res).to.have.status(123);
@@ -198,7 +229,7 @@ it('fails, as expected', function(done) { // <= Pass in done callback
198229
});
199230

200231
it('succeeds silently!', () => { // <= No done callback
201-
chai.request.execute('http://localhost:8080')
232+
request.execute('http://localhost:8080')
202233
.get('/')
203234
.end((err, res) => {
204235
expect(res).to.have.status(123); // <= Test completes before this runs
@@ -212,11 +243,13 @@ error parameter when signaling completion.
212243

213244
#### Dealing with the response - Promises
214245

215-
If `Promise` is available, `request()` becomes a Promise capable library -
246+
If `Promise` is available, `request` becomes a Promise capable library -
216247
and chaining of `then`s becomes possible:
217248

218249
```js
219-
chai.request.execute(app)
250+
import {request} from 'chai-http';
251+
252+
request.execute(app)
220253
.put('/user/me')
221254
.send({ password: '123', confirmPassword: '123' })
222255
.then((res) => {
@@ -233,8 +266,10 @@ Sometimes you need to keep cookies from one request, and send them with the
233266
next (for example, when you want to login with the first request, then access an authenticated-only resource later). For this, `.request.agent()` is available:
234267

235268
```js
269+
import {request} from 'chai-http';
270+
236271
// Log in
237-
const agent = chai.request.agent(app)
272+
const agent = request.agent(app)
238273
agent
239274
.post('/session')
240275
.send({ username: 'me', password: '123' })
@@ -249,7 +284,7 @@ agent
249284
});
250285
```
251286

252-
Note: The server started by `chai.request.agent(app)` will not automatically close following the test(s). You should call `agent.close()` after your tests to ensure your program exits.
287+
Note: The server started by `request.agent(app)` will not automatically close following the test(s). You should call `agent.close()` after your tests to ensure your program exits.
253288

254289
## Assertions
255290

package-lock.json

+4-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656
"is-ip": "^5.0.1",
5757
"methods": "^1.1.2",
5858
"picoquery": "^1.4.0",
59+
"@types/superagent": "^8.1.7",
5960
"superagent": "^9"
6061
},
6162
"devDependencies": {
6263
"@eslint/js": "^9.3.0",
6364
"@types/chai": "^4.3.16",
64-
"@types/superagent": "^8.1.7",
6565
"chai": "^5.1.0",
6666
"coveralls": "^3.1.1",
6767
"eslint": "^9.3.0",

test/bootstrap/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as originalChai from 'chai';
22
import * as http from 'http';
33
// this import is available from defining `imports` in package.json
4-
import project, { request } from 'chai-http';
4+
import {default as project, request } from 'chai-http';
55

66
global.http = http;
77

types/index.d.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Austin Cawley-Edwards <https://github.com/austince>
66
// TypeScript Version: 3.0
77
/// <reference types="chai" />
8-
import * as request from 'superagent';
8+
import * as superAgentRequest from 'superagent';
99

1010
// Merge namespace with global chai
1111
declare global {
@@ -48,8 +48,8 @@ declare global {
4848
}
4949

5050
namespace ChaiHttp {
51-
interface Response extends request.Response {}
52-
interface Agent extends request.SuperAgentStatic {
51+
interface Response extends superAgentRequest.Response {}
52+
interface Agent extends superAgentRequest.SuperAgentStatic {
5353
keepOpen(): Agent;
5454
close(callback?: (err: any) => void): Agent;
5555
}
@@ -59,3 +59,7 @@ declare global {
5959
declare function chaiHttp(chai: any, utils: any): void;
6060

6161
export default chaiHttp;
62+
63+
declare const request: Chai.ChaiHttpRequest;
64+
65+
export {request};

0 commit comments

Comments
 (0)