Skip to content

Commit

Permalink
chore: fixes tests for @middy/error-logger
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Aug 25, 2023
1 parent 49c5686 commit 70991f6
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/error-logger/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import test from 'ava'
import sinon from 'sinon'
import middy from '../../core/index.js'
import errorLogger from '../index.js'

Expand All @@ -10,19 +9,31 @@ const defaultContext = {

test('It should log errors and propagate the error', async (t) => {
const error = new Error('something bad happened')
const logger = sinon.spy()

let loggerCalledResolve = null
let loggerRequestReceived = null
const loggerHasBeenCalled = new Promise((resolve) => {
loggerCalledResolve = resolve
})

const mockLogger = (request) => {
loggerRequestReceived = request
loggerCalledResolve()
}

const handler = middy(() => {
throw error
})

handler.use(errorLogger({ logger }))
handler.use(errorLogger({ logger: mockLogger }))

try {
await handler(defaultEvent, defaultContext)
} catch (e) {
t.true(logger.calledWith(error))
t.deepEqual(e, error)
// the call to the logger is async so we need to make sure the invocation is complete
// before checking
await loggerHasBeenCalled
t.deepEqual(loggerRequestReceived.error, error)
}
})

Expand Down

0 comments on commit 70991f6

Please sign in to comment.