Skip to content

Commit 37e0dd8

Browse files
committed
cleanup #402
1 parent 4d96a3e commit 37e0dd8

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

docs/recipes/endpoint-testing.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
#Endpoint Testing
1+
# Endpoint testing
22

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-
```
3+
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).
4+
5+
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:
6+
7+
```js
68
function makeApp() {
7-
const app = express();
8-
app.post('/signup', signupHandler);
9-
return app;
9+
const app = express();
10+
app.post('/signup', signupHandler);
11+
return app;
1012
}
1113
```
1214

1315
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-
```
16+
17+
```js
1518
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: 'ava@rocks.com', password: '123123'})
21-
t.is(res.status, 200);
22-
t.is(res.body.email, 'ava@rocks.com');
19+
t.plan(2);
20+
21+
const res = await request(makeApp())
22+
.post('/signup')
23+
.send({email: 'ava@rocks.com', password: '123123'});
24+
25+
t.is(res.status, 200);
26+
t.is(res.body.email, 'ava@rocks.com');
2327
});
2428
```

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,9 +700,10 @@ AVA, not Ava or ava. Pronounced [`/ˈeɪvə/` ay-və](media/pronunciation.m4a?ra
700700

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

703+
703704
## Recipes
704705

705-
- [Endpoint testing](/docs/recipes/endpoint-testing.md)
706+
- [Endpoint testing](docs/recipes/endpoint-testing.md)
706707

707708

708709
## Support

0 commit comments

Comments
 (0)