UNPKG

1.37 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.enableErrorOutputSuppression = enableErrorOutputSuppression;
7exports.suppressErrorOutput = suppressErrorOutput;
8const consoleFilters = [/^The above error occurred in the <.*?> component:/, // error boundary output
9/^Error: Uncaught .+/ // jsdom output
10];
11
12function suppressErrorOutput() {
13 const originalError = console.error;
14
15 const error = (...args) => {
16 const message = typeof args[0] === 'string' ? args[0] : null;
17
18 if (!message || !consoleFilters.some(filter => filter.test(message))) {
19 originalError(...args);
20 }
21 };
22
23 console.error = error;
24 return () => {
25 console.error = originalError;
26 };
27}
28
29function errorFilteringDisabled() {
30 try {
31 return !!process.env.RHTL_DISABLE_ERROR_FILTERING;
32 } catch {
33 // falling back in the case that process.env.RHTL_DISABLE_ERROR_FILTERING cannot be accessed (e.g. browser environment)
34 return false;
35 }
36}
37
38function enableErrorOutputSuppression() {
39 // Automatically registers console error suppression and restoration in supported testing frameworks
40 if (typeof beforeEach === 'function' && typeof afterEach === 'function' && !errorFilteringDisabled()) {
41 let restoreConsole;
42 beforeEach(() => {
43 restoreConsole = suppressErrorOutput();
44 });
45 afterEach(() => restoreConsole());
46 }
47}
\No newline at end of file