-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathconsole.ts
49 lines (40 loc) · 1.26 KB
/
console.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const consoleFilters = [
/^The above error occurred in the <.*?> component:/, // error boundary output
/^Error: Uncaught .+/ // jsdom output
]
function suppressErrorOutput() {
const originalError = console.error
const error = (...args: Parameters<typeof originalError>) => {
const message = typeof args[0] === 'string' ? args[0] : null
if (!message || !consoleFilters.some((filter) => filter.test(message))) {
originalError(...args)
}
}
console.error = error
return () => {
console.error = originalError
}
}
function errorFilteringDisabled() {
try {
return !!process.env.RHTL_DISABLE_ERROR_FILTERING
} catch {
// falling back in the case that process.env.RHTL_DISABLE_ERROR_FILTERING cannot be accessed (e.g. browser environment)
return false
}
}
function enableErrorOutputSuppression() {
// Automatically registers console error suppression and restoration in supported testing frameworks
if (
typeof beforeEach === 'function' &&
typeof afterEach === 'function' &&
!errorFilteringDisabled()
) {
let restoreConsole!: () => void
beforeEach(() => {
restoreConsole = suppressErrorOutput()
})
afterEach(() => restoreConsole())
}
}
export { enableErrorOutputSuppression, suppressErrorOutput }