From 4d96a3e7e49280a7094be9751d446f76e2027cd0 Mon Sep 17 00:00:00 2001 From: mattkrick Date: Sat, 9 Jan 2016 02:45:17 +0100 Subject: [PATCH] Close #402 PR: add endpoint testing example to FAQ. Fixes #310 --- docs/recipes/endpoint-testing.md | 24 ++++++++++++++++++++++++ readme.md | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 docs/recipes/endpoint-testing.md diff --git a/docs/recipes/endpoint-testing.md b/docs/recipes/endpoint-testing.md new file mode 100644 index 000000000..05de4690b --- /dev/null +++ b/docs/recipes/endpoint-testing.md @@ -0,0 +1,24 @@ +#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: +``` +function makeApp() { + 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: +``` +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'); +}); +``` diff --git a/readme.md b/readme.md index 9a4ac4f85..802c270d8 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,7 @@ Translations: [Español](https://github.com/sindresorhus/ava-docs/blob/master/es - [API](#api) - [Assertions](#assertions) - [FAQ](#faq) +- [Recipes](#recipes) ## Why AVA? @@ -699,6 +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) + ## Support