-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
unhandledrejection
event shape and add more tests
- Loading branch information
1 parent
cc79970
commit d32379d
Showing
5 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'edge-runtime': patch | ||
--- | ||
|
||
Fix `unhandledrejection` handler parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |