Skip to content

Commit

Permalink
Fix #130 - Error in test logs
Browse files Browse the repository at this point in the history
I narrowed down these tests to aborting in a Koa app. This happened
because:

1. Koa listens for the error event on the HTTP stream itself, and
   re-emits it on the app's own event emitter.
2. If there is no listener for an app error, Koa will deviate from
   the node default of crashing, and will instead log the error.
3. This particular error originates from within the HTTP stream,
   causing Koa to log the error message.

I fixed this by actually _expecting_ one error to be emit from the
app, adding an assertion to test this.
  • Loading branch information
mike-marcacci committed Oct 7, 2019
1 parent 46c9513 commit a341dc1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/processRequest.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ t.test('Handles unconsumed uploads.', async t => {
})
})

t.test('Aborted request.', async t => {
t.only('Aborted request.', async t => {
const sendRequest = (port, requestHasBeenReceived) =>
new Promise((resolve, reject) => {
const body = new FormData()
Expand Down Expand Up @@ -720,7 +720,7 @@ t.test('Aborted request.', async t => {
}

await t.test('Koa middleware.', async t => {
t.plan(5)
t.plan(6)

let requestHasBeenReceived
const requestHasBeenReceivedPromise = new Promise(
Expand Down Expand Up @@ -757,6 +757,9 @@ t.test('Aborted request.', async t => {
finish()
})

let appErrors = 0
app.on('error', () => appErrors++)

const port = await startServer(t, app)

await sendRequest(port, requestHasBeenReceivedPromise)
Expand All @@ -771,6 +774,8 @@ t.test('Aborted request.', async t => {
if (!fileB.capacitor.closed)
await new Promise(resolve => fileB.capacitor.once('close', resolve))
t.false(fs.existsSync(fileB.capacitor.path), 'Cleanup B.')

await t.equals(appErrors, 1)
})

await t.test('Express middleware.', async t => {
Expand Down Expand Up @@ -851,7 +856,7 @@ t.test('Aborted request.', async t => {
}

await t.test('Koa middleware.', async t => {
t.plan(5)
t.plan(6)

let requestHasBeenReceived
const requestHasBeenReceivedPromise = new Promise(
Expand Down Expand Up @@ -893,7 +898,11 @@ t.test('Aborted request.', async t => {
finish()
})

let appErrors = 0
app.on('error', () => appErrors++)

const port = await startServer(t, app)

await sendRequest(port, requestHasBeenReceivedPromise)
await finished

Expand All @@ -906,6 +915,8 @@ t.test('Aborted request.', async t => {
if (!fileB.capacitor.closed)
await new Promise(resolve => fileB.capacitor.once('close', resolve))
t.false(fs.existsSync(fileB.capacitor.path), 'Cleanup B.')

t.equals(appErrors, 1)
})

await t.test('Express middleware.', async t => {
Expand Down

0 comments on commit a341dc1

Please sign in to comment.