Skip to content

Commit

Permalink
test: Refactor some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nwoltman committed Oct 20, 2019
1 parent c49bddb commit e6e7801
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 86 deletions.
33 changes: 13 additions & 20 deletions test/async-await.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const {test} = require('tap')
const request = require('./utils/request')
const medley = require('..')
const statusCodes = require('http').STATUS_CODES

function sleep(ms) {
return new Promise((resolve) => {
Expand All @@ -24,7 +23,7 @@ const opts = {
},
}

test('async await', (t) => {
test('async handlers can return the response body', (t) => {
t.plan(8)

const app = medley()
Expand Down Expand Up @@ -53,7 +52,7 @@ test('async await', (t) => {
})
})

test('should throw if an async function returns a value and res.send() is also called', (t) => {
test('`res.send()` should throw if called after an async handler returns a value', (t) => {
t.plan(3)

const app = medley()
Expand All @@ -74,7 +73,7 @@ test('should throw if an async function returns a value and res.send() is also c
})
})

test('should throw if an async function returns a value and res.error() is also called', (t) => {
test('`res.error()` should throw if called after an async handler returns a value', (t) => {
t.plan(3)

const app = medley()
Expand Down Expand Up @@ -111,28 +110,22 @@ test('Allows responding with `res.send()` inside of an async function', (t) => {
})
})

test('thrown Error in handler sets HTTP status code', (t) => {
test('Errors thrown inside an async route handler are caught and handled', (t) => {
t.plan(3)

const app = medley()

const err = new Error('winter is coming')
err.statusCode = 418

app.get('/', async () => {
throw err
throw new Error('kaboom')
})

request(app, '/', (error, res) => {
t.error(error)
t.strictEqual(res.statusCode, 418)
t.strictDeepEqual(
{
error: statusCodes['418'],
message: err.message,
statusCode: 418,
},
JSON.parse(res.body)
)
request(app, '/', (err, res) => {
t.error(err)
t.equal(res.statusCode, 500)
t.strictSame(JSON.parse(res.body), {
error: 'Internal Server Error',
message: 'kaboom',
statusCode: 500,
})
})
})
4 changes: 2 additions & 2 deletions test/hooks-onError.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ test('encapsulated onError hook', (t) => {

const app = medley()

app.get('/', function(req, response) {
response.error(new Error('kaboom'))
app.get('/', function(req, res) {
res.error(new Error('kaboom'))
})

app.createSubApp('/test')
Expand Down
48 changes: 0 additions & 48 deletions test/internals/Serializer.test.js

This file was deleted.

27 changes: 11 additions & 16 deletions test/serialization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const opts = {
},
},
},
201: { // shorthand
201: { // Schema shorthand
hello: {
type: 'number',
},
Expand All @@ -34,65 +34,60 @@ app.get('/wrong-object-for-schema', opts, (req, res) => {
res.status(201).send({uno: 1}) // Will send { }
})

// No checks
// Status code does not match schema
app.get('/empty', opts, (req, res) => {
res.status(204).send()
})

app.get('/400', opts, (req, res) => {
res.status(400).send({hello: 'DOOM'})
})

test('shorthand - string get ok', (t) => {
test('Full object schema', (t) => {
t.plan(4)

request(app, '/string', (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 200)
t.strictEqual(res.headers['content-length'], '' + res.body.length)
t.deepEqual(JSON.parse(res.body), {hello: 'world'})
t.strictSame(JSON.parse(res.body), {hello: 'world'})
})
})

test('shorthand - number get ok', (t) => {
test('Shorthand object schema', (t) => {
t.plan(4)

request(app, '/number', (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 201)
t.strictEqual(res.headers['content-length'], '' + res.body.length)
t.deepEqual(JSON.parse(res.body), {hello: 55})
t.strictSame(JSON.parse(res.body), {hello: 55})
})
})

test('shorthand - wrong-object-for-schema', (t) => {
test('Serializer omits properties that do not match the schema', (t) => {
t.plan(4)

request(app, '/wrong-object-for-schema', (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 201)
t.strictEqual(res.headers['content-length'], '' + res.body.length)
t.deepEqual(JSON.parse(res.body), {})
t.strictSame(JSON.parse(res.body), {})
})
})

test('shorthand - empty', (t) => {
t.plan(3)
test('Responses that do not match a schema status code are not affected', (t) => {
t.plan(7)

request(app, '/empty', (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 204)
t.strictEqual(res.body, '')
})
})

test('shorthand - 400', (t) => {
t.plan(4)

request(app, '/400', (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 400)
t.strictEqual(res.headers['content-length'], '' + res.body.length)
t.deepEqual(JSON.parse(res.body), {hello: 'DOOM'})
t.strictSame(JSON.parse(res.body), {hello: 'DOOM'})
})
})

0 comments on commit e6e7801

Please sign in to comment.