vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Support ignoring specific unhandled errors

Open simonihmig opened this issue 9 months ago • 1 comments

Clear and concise description of the problem

In browser mode, I would like to be able to ignore certain unhandled errors, without resorting to ignore all by using dangerouslyIgnoreUnhandledErrors.

Suggested solution

I was trying something along the lines of

beforeAll(() => {
  window.addEventListener('error', (error) => {
    if (shouldIgnore(error)) {
      console.error(error.message);
    } else {
      throw error;
    }
  });
});

While this works for the part that unhandled errors are indeed ignored, it does however ignore all errors, also those that are re-thrown and shouldn't be ignored.

Looking at this code, it seems once any custom error listeners have been added, there is no way that errors get reported back, they are only printed to the console.

I would be nice if a pattern like the one above could be supported.

Alternative

Alternatively, a custom config option could be introduced, where a callback would be able to filter the errors, like:

ignoreUnhandledErrors: (error: Error) => boolean;

Additional context

My use case here is that we have ResizeObservers that in same cases throw errors like ResizeObserver loop limit exceeded or ResizeObserver loop completed with undelivered notifications.. For whatever reason, it seems the spec chose to throw these as errors rather than just printing to the console.

They might indicate performance issues, but can often be ignored, certainly shouldn't make tests fail. This is giving us quite some headaches with flakey tests.

Validations

simonihmig avatar Mar 14 '25 17:03 simonihmig

One possibility I might suggest would be to add a 'runDangerouslyoption in theTestCollectorOptionparameter for individual tests and for 'TestOptions' in thedescribe` function.

To me, having the ability to run part of the tests "dangerously" would allow some level of isolation rather than having to run everything dangerously. (Of course, you guys would be better able to imagine the implications of this idea.)

dagilleland avatar May 26 '25 00:05 dagilleland