Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add endpoint testing example to FAQ #402

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/recipes/endpoint-testing.md
Original file line number Diff line number Diff line change
@@ -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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but an empty line between these (after the title and the text),

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:
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add empty line.

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:
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do, PS anyone know a way to squash commits within github without doing the pull/rebase/push dance?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's possible afaik.

test('signup:Success', async t => {
t.plan(2);
const app = makeApp();
const res = await request(app)
.post('/signup')
.send({email: '[email protected]', password: '123123'})
t.is(res.status, 200);
t.is(res.body.email, '[email protected]');
});
```
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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

Expand Down