-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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). | ||
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: | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add empty line. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]'); | ||
}); | ||
``` |
There was a problem hiding this comment.
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),