Skip to content

Instantly share code, notes, and snippets.

@exequiel09
Created December 5, 2025 11:37
Show Gist options
  • Select an option

  • Save exequiel09/3eb99dfcdcfbc0d1fdb3bef4d538d79d to your computer and use it in GitHub Desktop.

Select an option

Save exequiel09/3eb99dfcdcfbc0d1fdb3bef4d538d79d to your computer and use it in GitHub Desktop.
Suppressing errors in Vitest per test
import { suppressError } from "./vitest-per-test-error-suppressor;
describe('Suppress Error', () => {
it('should suppress error', suppressError((error) => error instanceof TypeError, () => {
expect(true).toBe(true);
}));
});
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