-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
a2b282e
commit 4d96a3e
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
``` | ||
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'); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters