forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnew-act.js
76 lines (67 loc) · 1.6 KB
/
new-act.js
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
76
let asyncAct
jest.mock('react-dom/test-utils', () => ({
act: cb => {
return cb()
},
}))
beforeEach(() => {
jest.resetModules()
asyncAct = require('../act-compat').asyncAct
jest.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
console.error.mockRestore()
})
test('async act works when it does not exist (older versions of react)', async () => {
const callback = jest.fn()
await asyncAct(async () => {
await Promise.resolve()
await callback()
})
expect(console.error).toHaveBeenCalledTimes(0)
expect(callback).toHaveBeenCalledTimes(1)
callback.mockClear()
console.error.mockClear()
await asyncAct(async () => {
await Promise.resolve()
await callback()
})
expect(console.error).toHaveBeenCalledTimes(0)
expect(callback).toHaveBeenCalledTimes(1)
})
test('async act recovers from errors', async () => {
try {
await asyncAct(async () => {
await null
throw new Error('test error')
})
} catch (err) {
console.error('call console.error')
}
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
call console.error,
],
]
`)
})
test('async act recovers from sync errors', async () => {
try {
await asyncAct(() => {
throw new Error('test error')
})
} catch (err) {
console.error('call console.error')
}
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
call console.error,
],
]
`)
})
/* eslint no-console:0 */