Skip to content

Commit 4d96a3e

Browse files
mattkricksindresorhus
authored andcommitted
Close #402 PR: add endpoint testing example to FAQ. Fixes #310
1 parent a2b282e commit 4d96a3e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

docs/recipes/endpoint-testing.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#Endpoint Testing
2+
3+
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).
4+
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:
5+
```
6+
function makeApp() {
7+
const app = express();
8+
app.post('/signup', signupHandler);
9+
return app;
10+
}
11+
```
12+
13+
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:
14+
```
15+
test('signup:Success', async t => {
16+
t.plan(2);
17+
const app = makeApp();
18+
const res = await request(app)
19+
.post('/signup')
20+
.send({email: '[email protected]', password: '123123'})
21+
t.is(res.status, 200);
22+
t.is(res.body.email, '[email protected]');
23+
});
24+
```

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Translations: [Español](https://github.com/sindresorhus/ava-docs/blob/master/es
2020
- [API](#api)
2121
- [Assertions](#assertions)
2222
- [FAQ](#faq)
23+
- [Recipes](#recipes)
2324

2425

2526
## Why AVA?
@@ -699,6 +700,10 @@ AVA, not Ava or ava. Pronounced [`/ˈeɪvə/` ay-və](media/pronunciation.m4a?ra
699700

700701
Concurrency is not parallelism. It enables parallelism. [Learn more.](http://stackoverflow.com/q/1050222)
701702

703+
## Recipes
704+
705+
- [Endpoint testing](/docs/recipes/endpoint-testing.md)
706+
702707

703708
## Support
704709

0 commit comments

Comments
 (0)