Skip to content

Instantly share code, notes, and snippets.

@indatawetrust
Last active May 16, 2020 13:56
Show Gist options
  • Save indatawetrust/5e1a882e05672bb3d3215af8d2dbf7ee to your computer and use it in GitHub Desktop.
Save indatawetrust/5e1a882e05672bb3d3215af8d2dbf7ee to your computer and use it in GitHub Desktop.
expressjs endpoint testing with ava + supertest + @hapi/joi
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