cleanup #402
This commit is contained in:
parent
4d96a3e7e4
commit
37e0dd84d2
2 changed files with 21 additions and 16 deletions
|
|
@ -1,24 +1,28 @@
|
|||
#Endpoint Testing
|
||||
# Endpoint testing
|
||||
|
||||
AVA doesn't have an official assertion library for endpoints, but a great option is [`supertest-as-promised`](https://github.com/WhoopInc/supertest-as-promised).
|
||||
Since the tests run concurrently, it's a best practice 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 `beforeEach` and `context`, or even more simply with a factory function:
|
||||
```
|
||||
AVA doesn't have a builtin method for testing endpoints, but you can use any assertion library with it. Let's use [`supertest-as-promised`](https://github.com/WhoopInc/supertest-as-promised).
|
||||
|
||||
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:
|
||||
|
||||
```js
|
||||
function makeApp() {
|
||||
const app = express();
|
||||
app.post('/signup', signupHandler);
|
||||
return app;
|
||||
const app = express();
|
||||
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 supertest's `end` method:
|
||||
```
|
||||
|
||||
```js
|
||||
test('signup:Success', async t => {
|
||||
t.plan(2);
|
||||
const app = makeApp();
|
||||
const res = await request(app)
|
||||
.post('/signup')
|
||||
.send({email: 'ava@rocks.com', password: '123123'})
|
||||
t.is(res.status, 200);
|
||||
t.is(res.body.email, 'ava@rocks.com');
|
||||
t.plan(2);
|
||||
|
||||
const res = await request(makeApp())
|
||||
.post('/signup')
|
||||
.send({email: 'ava@rocks.com', password: '123123'});
|
||||
|
||||
t.is(res.status, 200);
|
||||
t.is(res.body.email, 'ava@rocks.com');
|
||||
});
|
||||
```
|
||||
|
|
|
|||
|
|
@ -700,9 +700,10 @@ AVA, not Ava or ava. Pronounced [`/ˈeɪvə/` ay-və](media/pronunciation.m4a?ra
|
|||
|
||||
Concurrency is not parallelism. It enables parallelism. [Learn more.](http://stackoverflow.com/q/1050222)
|
||||
|
||||
|
||||
## Recipes
|
||||
|
||||
- [Endpoint testing](/docs/recipes/endpoint-testing.md)
|
||||
- [Endpoint testing](docs/recipes/endpoint-testing.md)
|
||||
|
||||
|
||||
## Support
|
||||
|
|
|
|||
Loading…
Reference in a new issue