forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathold-act.js
142 lines (129 loc) · 3.61 KB
/
old-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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
let asyncAct, consoleErrorMock
beforeEach(() => {
jest.resetModules()
asyncAct = require('../act-compat').asyncAct
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
consoleErrorMock.mockRestore()
})
jest.mock('react-dom/test-utils', () => ({
act: cb => {
cb()
return {
then() {
console.error(
'Warning: Do not await the result of calling ReactTestUtils.act(...), it is not a Promise.',
)
},
}
},
}))
test('async act works even when the act is an old one', async () => {
const callback = jest.fn()
await asyncAct(async () => {
console.error('sigil')
await Promise.resolve()
await callback()
console.error('sigil')
})
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
sigil,
],
Array [
It looks like you're using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least [email protected] to remove this warning.,
],
Array [
sigil,
],
]
`)
expect(callback).toHaveBeenCalledTimes(1)
// and it doesn't warn you twice
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 async errors', async () => {
try {
await asyncAct(async () => {
await null
throw new Error('test error')
})
} catch (err) {
console.error('call console.error')
}
expect(console.error).toHaveBeenCalledTimes(2)
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
It looks like you're using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least [email protected] to remove this warning.,
],
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,
],
]
`)
})
test('async act can handle any sort of console.error', async () => {
await asyncAct(async () => {
console.error({error: 'some error'})
await null
})
expect(console.error).toHaveBeenCalledTimes(2)
expect(console.error.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Object {
error: some error,
},
],
Array [
It looks like you're using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least [email protected] to remove this warning.,
],
]
`)
})
test('async act should not show an error when ReactTestUtils.act returns something', async () => {
jest.resetModules()
jest.mock('react-dom/test-utils', () => ({
act: () => {
return new Promise(resolve => {
console.error(
'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything',
)
resolve()
})
},
}))
asyncAct = require('../act-compat').asyncAct
await asyncAct(async () => {
await null
})
expect(console.error).toHaveBeenCalledTimes(0)
})
/* eslint no-console:0 */