throw-on-react-error.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // eslint-disable-next-line no-console
  2. const originalConsoleError = console.error;
  3. // List of `console.error` messages to ignore
  4. // (i.e. don't fail tests when we get these messages)
  5. const IGNORED_ERRORS = [
  6. (message, ...args) =>
  7. typeof message === 'string' &&
  8. message.includes('Warning: ') &&
  9. args[0] === 'CreatableSelect',
  10. message =>
  11. typeof message === 'string' &&
  12. message.includes(
  13. 'The pseudo class ":first-child" is potentially unsafe when doing server-side rendering.'
  14. ),
  15. message =>
  16. typeof message === 'string' && message.includes('HTMLMediaElement.prototype.play'),
  17. ];
  18. // This is needed because when we throw the captured error message, it will
  19. // also `console.error` it
  20. const REPEATED_ERROR = 'Error: Uncaught [Error: ';
  21. jest.spyOn(console, 'error').mockImplementation((message, ...args) => {
  22. const isIgnored = IGNORED_ERRORS.some(checkFn => checkFn(message, ...args));
  23. if (
  24. typeof message === 'string' &&
  25. message.indexOf(REPEATED_ERROR) !== 0 &&
  26. !isIgnored
  27. ) {
  28. originalConsoleError(message, ...args);
  29. const err = new Error('Warnings received from console.error()');
  30. const lines = err.stack?.split('\n');
  31. const startIndex = lines?.findIndex(line => line.includes('tests/js/spec'));
  32. err.stack = ['\n', lines?.[0], ...lines?.slice(startIndex)].join('\n');
  33. throw err;
  34. }
  35. });