-
Notifications
You must be signed in to change notification settings - Fork 231
/
errorSuppression.test.ts
75 lines (61 loc) 路 2.19 KB
/
errorSuppression.test.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { useEffect } from 'react'
import { ReactHooksRenderer } from '../../types/react'
describe('error output suppression tests', () => {
test('should not suppress relevant errors', () => {
const consoleError = console.error
console.error = jest.fn()
const { suppressErrorOutput } = require('..') as ReactHooksRenderer
try {
const restoreConsole = suppressErrorOutput()
console.error('expected')
console.error(new Error('expected'))
console.error('expected with args', new Error('expected'))
restoreConsole()
expect(console.error).toBeCalledWith('expected')
expect(console.error).toBeCalledWith(new Error('expected'))
expect(console.error).toBeCalledWith('expected with args', new Error('expected'))
expect(console.error).toBeCalledTimes(3)
} finally {
console.error = consoleError
}
})
test('should allow console.error to be mocked', async () => {
const { renderHook, act } = require('..') as ReactHooksRenderer
const consoleError = console.error
console.error = jest.fn()
try {
const { rerender, unmount } = renderHook(
(stage) => {
useEffect(() => {
console.error(`expected in effect`)
return () => {
console.error(`expected in unmount`)
}
}, [])
console.error(`expected in ${stage}`)
},
{
initialProps: 'render'
}
)
act(() => {
console.error('expected in act')
})
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 100))
console.error('expected in async act')
})
rerender('rerender')
unmount()
expect(console.error).toBeCalledWith('expected in render')
expect(console.error).toBeCalledWith('expected in effect')
expect(console.error).toBeCalledWith('expected in act')
expect(console.error).toBeCalledWith('expected in async act')
expect(console.error).toBeCalledWith('expected in rerender')
expect(console.error).toBeCalledWith('expected in unmount')
expect(console.error).toBeCalledTimes(6)
} finally {
console.error = consoleError
}
})
})