Last active
May 16, 2020 13:56
-
-
Save indatawetrust/5e1a882e05672bb3d3215af8d2dbf7ee to your computer and use it in GitHub Desktop.
expressjs endpoint testing with ava + supertest + @hapi/joi
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
const test = require('ava') | |
const request = require('supertest') | |
const app = require('../lib/infra') | |
const Joi = require('@hapi/joi') | |
const agent = request.agent(app) | |
test('main', async (t) => { | |
const { body } = await agent.get('/') | |
t.deepEqual(body, { api: 'v1' }) | |
}) | |
test('users / find', async (t) => { | |
const responseSchema = Joi.object({ | |
data: Joi.array() | |
.items( | |
Joi.object({ | |
username: Joi.string() | |
}) | |
) | |
.required() | |
}) | |
const { body } = await agent.get('/users') | |
await responseSchema.validateAsync(body) | |
t.pass() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment