-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vitest): allow testing unandled rejection/exception (#6016)
- Loading branch information
1 parent
eb6ea5b
commit c8d56fe
Showing
2 changed files
with
54 additions
and
3 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
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,40 @@ | ||
import { nextTick } from 'node:process' | ||
import { expect, test, vi } from 'vitest' | ||
|
||
test('can test unhandled rejection', async () => { | ||
const fn = vi.fn() | ||
|
||
const promise = new Promise<void>((resolve) => { | ||
process.on('unhandledRejection', () => { | ||
fn() | ||
resolve() | ||
}) | ||
}) | ||
|
||
Promise.resolve().then(() => { | ||
throw new Error('unhandled rejection') | ||
}) | ||
|
||
await promise | ||
|
||
expect(fn).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('can test unhandled exception', async () => { | ||
const fn = vi.fn() | ||
|
||
const promise = new Promise<void>((resolve) => { | ||
process.on('uncaughtException', () => { | ||
fn() | ||
resolve() | ||
}) | ||
}) | ||
|
||
nextTick(() => { | ||
throw new Error('unhandled exception') | ||
}) | ||
|
||
await promise | ||
|
||
expect(fn).toHaveBeenCalledTimes(1) | ||
}) |