-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update endpoint testing recipe to focus on the concept, not libraries
- Loading branch information
1 parent
4fdb02d
commit 67e4dea
Showing
1 changed file
with
24 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
novemberborn
Member
|
||
|
||
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: '[email protected]', 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, '[email protected]'); | ||
}); | ||
``` | ||
|
||
Other libraries you may find useful: | ||
|
||
- [`supertest`](https://github.com/visionmedia/supertest) | ||
- [`get-port`](https://github.com/sindresorhus/get-port) | ||
|
Why use the
test-listen
library? Without extra dependencies by passing0
to thelisten
method which will also assign an available port.