Created
December 5, 2025 11:37
-
-
Save exequiel09/3eb99dfcdcfbc0d1fdb3bef4d538d79d to your computer and use it in GitHub Desktop.
Suppressing errors in Vitest per test
This file contains hidden or 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
| import { suppressError } from "./vitest-per-test-error-suppressor; | |
| describe('Suppress Error', () => { | |
| it('should suppress error', suppressError((error) => error instanceof TypeError, () => { | |
| expect(true).toBe(true); | |
| })); | |
| }); |
This file contains hidden or 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
| import { TestContext, TestFunction } from 'vitest'; | |
| export const suppressError = | |
| (errorFilterFn: (error: unknown) => boolean, testFn: TestFunction) => | |
| (context: TestContext) => | |
| new Promise<void>((resolve, reject) => { | |
| const handler = (error: unknown) => { | |
| if (errorFilterFn(error)) { | |
| return; | |
| } | |
| reject(error); | |
| }; | |
| process.on('uncaughtException', handler); | |
| context.onTestFinished(() => { | |
| process.off('unhandledRejection', handler); | |
| }); | |
| testFn(context); | |
| resolve(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment