Skip to content

Commit

Permalink
Fix unhandledrejection event shape and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed Sep 27, 2022
1 parent cc79970 commit d32379d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-papayas-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'edge-runtime': patch
---

Fix `unhandledrejection` handler parameters
4 changes: 3 additions & 1 deletion packages/runtime/src/edge-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export class EdgeRuntime<
process.on(
'unhandledRejection',
function invokeRejectionHandlers(reason, promise) {
unhandledRejectionHandlers?.forEach((handler) => handler(reason, promise))
unhandledRejectionHandlers?.forEach((handler) =>
handler({ reason, promise })
)
}
)
process.on('uncaughtException', function invokeErrorHandlers(error) {
Expand Down
7 changes: 4 additions & 3 deletions packages/runtime/tests/edge-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ beforeAll(async () => {
.spyOn(process, 'on')
.mockImplementation((event: string, handler: Function) => {
handlerByEvent.set(event, handler)
return process
})
;({ EdgeRuntime } = await import('../src/edge-runtime'))
})
Expand Down Expand Up @@ -60,13 +61,13 @@ test('invokes context rejection handler', async () => {
const runtime = new EdgeRuntime()

const handleRejection = jest.fn()
const error = new Error('Boom!!!')
const reason = new Error('Boom!!!')
const promise = Promise.resolve()
runtime.context.addEventListener('unhandledrejection', handleRejection)

expect(handlerByEvent.get('unhandledRejection')).toBeDefined()
handlerByEvent.get('unhandledRejection')!(error, promise)
expect(handleRejection).toHaveBeenCalledWith(error, promise)
handlerByEvent.get('unhandledRejection')!(reason, promise)
expect(handleRejection).toHaveBeenCalledWith({ reason, promise })
expect(handleRejection).toHaveBeenCalledTimes(1)
})

Expand Down
14 changes: 14 additions & 0 deletions packages/runtime/tests/unhandled-rejection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { exec } from 'child_process'
import { promisify } from 'util'
import { resolve } from 'path'

jest.setTimeout(20000)

it('handles correctly unhandled rejections', async () => {
const execAsync = promisify(exec)
const result = await execAsync(
`ts-node --transpile-only ${resolve(__dirname, 'unhandled-rejection.ts')}`,
{ encoding: 'utf8' }
)
expect(result.stdout).toContain('TEST PASSED!')
})
37 changes: 37 additions & 0 deletions packages/runtime/tests/unhandled-rejection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { EdgeRuntime } from '../src'
import assert from 'assert'

async function main() {
const runtime = new EdgeRuntime()
const deferred = new Promise<PromiseRejectionEvent>((resolve) => {
runtime.context.handleRejection = (event: PromiseRejectionEvent) => {
resolve(event)
}
})

runtime.evaluate(`
addEventListener('fetch', (event) => {
new Promise((resolve, reject) => reject(new TypeError('This is not controlled')))
event.respondWith(new Response('hello'));
})
addEventListener('unhandledrejection', (event) => {
globalThis.handleRejection(event)
})
`)

const response = await runtime.dispatchFetch('https://example.com')
const event = await deferred

assert.strictEqual(response.status, 200)
assert.strictEqual(event.reason.message, 'This is not controlled')
return 'TEST PASSED!'
}

main()
.then(console.log)
.catch((error) => {
console.log('TEST FAILED!')
console.log(error)
process.exit(1)
})

0 comments on commit d32379d

Please sign in to comment.