Skip to content

Commit

Permalink
Update endpoint testing recipe to focus on the concept, not libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
tryzniak authored and novemberborn committed Sep 15, 2019
1 parent 4fdb02d commit 67e4dea
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions docs/recipes/endpoint-testing.md
Expand Up @@ -2,30 +2,36 @@

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)

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

This comment has been minimized.

Copy link
@Siilwyn

Siilwyn Oct 12, 2019

Why use the test-listen library? Without extra dependencies by passing 0 to the listen method which will also assign an available port.

This comment has been minimized.

Copy link
@novemberborn

novemberborn Oct 13, 2019

Member

test-listen returns a promise, and a URL you can use to connect to the server.


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:
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.

```js
function makeApp() {
const app = express();
app.use(bodyParser.json());
app.post('/signup', signupHandler);
return app;
}
```

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:
Check out the example below:

```js
test('signup:Success', async t => {
t.plan(2);
const http = require('http');
const test = require('ava');
const got = require('got');
const listen = require('test-listen');
const app = require('../app');

test.before(async t => {
t.context.server = http.createServer(app);
t.context.baseURL = await listen(t.context.server);
});

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

t.is(res.status, 200);
test.serial('get /user', async t => {
const res = await got('/user', { baseURL: t.context.baseURL, json: true });
t.is(res.body.email, 'ava@rocks.com');
});
```

Other libraries you may find useful:

- [`supertest`](https://github.com/visionmedia/supertest)
- [`get-port`](https://github.com/sindresorhus/get-port)

0 comments on commit 67e4dea

Please sign in to comment.