Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💯 test coverage #190

Merged
merged 1 commit into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions test/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Packages
const test = require('ava')
const request = require('request-promise')
const listen = require('test-listen')

process.env.NODE_ENV = 'development'
const micro = require('../lib/server')

const getUrl = fn => {
const srv = micro(fn)

return listen(srv)
}

test('send(200, <Object>) is pretty-printed', async t => {
const fn = () => {
return {woot: 'yes'}
}

const url = await getUrl(fn)
const res = await request(url)

t.deepEqual(res, `{\n "woot": "yes"\n}`)
})
45 changes: 38 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,27 @@ test('json limit (below)', async t => {

test('json limit (over)', async t => {
const fn = async (req, res) => {
let body

try {
body = await json(req, {
await json(req, {
limit: 3
})
} catch (err) {
t.deepEqual(err.statusCode, 413)
}

send(res, 200, {
response: body.some.cool
})
send(res, 200, 'ok')
}

await getUrl(fn)
const url = await getUrl(fn)
await request(url, {
method: 'POST',
body: {
some: {
cool: 'json'
}
},
json: true
})
})

test('json circular', async t => {
Expand Down Expand Up @@ -477,3 +482,29 @@ test('support for status fallback in errors', async t => {
t.deepEqual(err.statusCode, 403)
}
})

test('json from rawBodyMap works', async t => {
const fn = async (req, res) => {
const bodyOne = await json(req)
const bodyTwo = await json(req)

t.deepEqual(bodyOne, bodyTwo)

send(res, 200, {
response: bodyOne.some.cool
})
}

const url = await getUrl(fn)
const body = await request(url, {
method: 'POST',
body: {
some: {
cool: 'json'
}
},
json: true
})

t.deepEqual(body.response, 'json')
})
34 changes: 34 additions & 0 deletions test/production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Packages
const test = require('ava')
const request = require('request-promise')
const listen = require('test-listen')

process.env.NODE_ENV = 'production'
const micro = require('../lib/server')

const getUrl = fn => {
const srv = micro(fn)

return listen(srv)
}

test.serial('errors are printed in console', async t => {
let logged = false
const _error = console.error
console.error = () => {
logged = true
}

const fn = () => {
throw new Error('Bang')
}

const url = await getUrl(fn)
try {
await request(url)
} catch (err) {
t.true(logged)
t.deepEqual(err.statusCode, 500)
console.error = _error
}
})