Skip to content

Commit 67e4dea

Browse files
tryzniaknovemberborn
authored andcommittedSep 15, 2019
Update endpoint testing recipe to focus on the concept, not libraries
1 parent 4fdb02d commit 67e4dea

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed
 

‎docs/recipes/endpoint-testing.md

+24-18
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,36 @@
22

33
Translations: [Español](https://github.com/avajs/ava-docs/blob/master/es_ES/docs/recipes/endpoint-testing.md), [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/endpoint-testing.md), [Italiano](https://github.com/avajs/ava-docs/blob/master/it_IT/docs/recipes/endpoint-testing.md), [日本語](https://github.com/avajs/ava-docs/blob/master/ja_JP/docs/recipes/endpoint-testing.md), [Português](https://github.com/avajs/ava-docs/blob/master/pt_BR/docs/recipes/endpoint-testing.md), [Русский](https://github.com/avajs/ava-docs/blob/master/ru_RU/docs/recipes/endpoint-testing.md), [简体中文](https://github.com/avajs/ava-docs/blob/master/zh_CN/docs/recipes/endpoint-testing.md)
44

5-
AVA doesn't have a builtin method for testing endpoints, but you can use any assertion library with it. Let's use [`supertest`](https://github.com/visionmedia/supertest).
5+
AVA doesn't have a built-in method for testing endpoints, but you can use any HTTP client of your choosing, for example [`got`](https://github.com/sindresorhus/got). You'll also need to start an HTTP server, preferrably on a unique port so that you can run tests in parallel. For that we recommend [`test-listen`](https://github.com/zeit/test-listen).
Has conversations. Original line has conversations.
66

7-
Since tests run concurrently, it's best to create a fresh server instance for each test, because if we referenced the same instance, it could be mutated between tests. This can be accomplished with a `test.beforeEach` and `t.context`, or with simply a factory function:
7+
Since tests run concurrently, it's best to create a fresh server instance at least for each test file, but perhaps even for each test. This can be accomplished with `test.before()` and `test.beforeEach()` hooks and `t.context`. If you start your server using a `test.before()` hook you should make sure to execute your tests serially.
88

9-
```js
10-
function makeApp() {
11-
const app = express();
12-
app.use(bodyParser.json());
13-
app.post('/signup', signupHandler);
14-
return app;
15-
}
16-
```
17-
18-
Next, just inject your server instance into supertest. The only gotcha is to use a promise or async/await syntax instead of the supertest `end` method:
9+
Check out the example below:
1910

2011
```js
21-
test('signup:Success', async t => {
22-
t.plan(2);
12+
const http = require('http');
13+
const test = require('ava');
14+
const got = require('got');
15+
const listen = require('test-listen');
16+
const app = require('../app');
17+
18+
test.before(async t => {
19+
t.context.server = http.createServer(app);
20+
t.context.baseURL = await listen(t.context.server);
21+
});
2322

24-
const res = await request(makeApp())
25-
.post('/signup')
26-
.send({email: 'ava@rocks.com', password: '123123'});
23+
test.after.always(t => {
24+
t.context.server.close();
25+
});
2726

28-
t.is(res.status, 200);
27+
test.serial('get /user', async t => {
28+
const res = await got('/user', { baseURL: t.context.baseURL, json: true });
2929
t.is(res.body.email, 'ava@rocks.com');
3030
});
3131
```
32+
33+
Other libraries you may find useful:
34+
35+
- [`supertest`](https://github.com/visionmedia/supertest)
36+
- [`get-port`](https://github.com/sindresorhus/get-port)
37+

0 commit comments

Comments
 (0)
Please sign in to comment.