Skip to content

Instantly share code, notes, and snippets.

@brandonaaskov
Last active January 17, 2019 20:23
Show Gist options
  • Save brandonaaskov/cc9e80181c88144fc2ff6a518df5d792 to your computer and use it in GitHub Desktop.
Save brandonaaskov/cc9e80181c88144fc2ff6a518df5d792 to your computer and use it in GitHub Desktop.
Integration tests for a basic CRUD API for Users
require('dotenv').config()
const { HOST, PORT } = process.env
const request = require('supertest')(`http://${HOST}:${PORT}`)
describe('Users CRUD APIs', () => {
let userId // gets stored once we create a user
const userInfo = {
name: 'Dade Murphy',
username: 'Zero Cool',
address: {
city: 'New York',
state: 'NY',
zip: '11363'
}
}
describe('Create User', () => {
it('POST /api/users', async () => {
const response = await request.post('/api/users')
.send(userInfo)
.expect(200)
userId = response.body._id
expect(userId).toBeDefined()
})
})
describe('Get User (after create)', () => {
it(`GET /api/users/:userId`, async () => {
const { body } = await request.get(`/api/users/${userId}`)
.send()
.expect(200)
expect(body).toEqual({
_id: userId,
...userInfo
})
})
})
describe('Update User', () => {
it('PUT /api/users/:userId', async () => {
const response = await request.put(`/api/users/${userId}`)
.send({ address: { zip: '11365' } })
.expect(200)
const expected = {
...userInfo,
_id: userId,
address: {
...userInfo.address,
zip: '11365'
}
}
expect(response.body).toEqual(expected)
})
})
describe('Delete User', () => {
it('DELETE /api/users/:userId', async () => {
await request.delete(`/api/users/${userId}`)
.send()
.expect(205)
})
})
describe('Get User (after delete)', () => {
it('GET /api/users/:userId', async () => {
await request.get(`/api/users/${userId}`)
.send()
.expect(404)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment