|  | 
| 2 | 2 | 
 | 
| 3 | 3 | 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) | 
| 4 | 4 | 
 | 
| 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). | 
| 6 | 6 | 
 | 
| 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. | 
| 8 | 8 | 
 | 
| 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: | 
| 19 | 10 | 
 | 
| 20 | 11 | ```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 | +}); | 
| 23 | 22 | 
 | 
| 24 |  | -	const res = await request(makeApp()) | 
| 25 |  | -		.post('/signup') | 
| 26 |  | -		.send({email: '[email protected]', password: '123123'}); | 
|  | 23 | +test.after.always(t => { | 
|  | 24 | +	t.context.server.close(); | 
|  | 25 | +}); | 
| 27 | 26 | 
 | 
| 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 }); | 
| 29 | 29 | 	t.is (res .body .email , '[email protected]' ); | 
| 30 | 30 | }); | 
| 31 | 31 | ``` | 
|  | 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