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: refactor descriptions #114

Merged
merged 1 commit into from
Mar 23, 2024
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
4 changes: 3 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ const { createServer } = require('http')
const closeServer = server =>
require('util').promisify(server.close.bind(server))()

const runServer = async (t, handler) => {
const runServer = async (t, handler, { throwErrors = true } = {}) => {
const server = createServer(async (req, res) => {
try {
await handler({ req, res })
} catch (error) {
console.error(error)
if (throwErrors) throw error
res.statusCode = 500
res.end()
}
})

const url = await listen(server)
t.teardown(() => closeServer(server))
return url
Expand Down
11 changes: 6 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('compress support', async t => {
t.is(headersOne.etag, headersTwo.etag)
})

test('exit early is get is empty', async t => {
test('exit early is response was written', async t => {
let isEnd = false
const end = (res, msg) => {
isEnd = true
Expand All @@ -73,21 +73,22 @@ test('exit early is get is empty', async t => {
t.is(res.body, 'get')
})

test('prevent send if data is undefined', async t => {
test('prevent send if get throws an error', async t => {
t.plan(1)
let isSendCalled = false
const url = await runServer(
t,
cacheableResponse({
compress: true,
get: ({ req, res }) => {
get: () => {
throw Error()
},
send: ({ data, headers, res, req, ...props }) => {
send: ({ res }) => {
isSendCalled = true
res.end('Hello World')
}
})
}),
{ throwErrors: false }
)
try {
await got(`${url}/kikobeats`, { retry: 0 })
Expand Down