|
1 | 1 | import { useState, useEffect } from 'react'
|
2 | 2 |
|
3 |
| -import { renderHook } from '..' |
| 3 | +import { renderHook, act } from '..' |
4 | 4 |
|
5 | 5 | describe('error hook tests', () => {
|
6 | 6 | function useError(throwError?: boolean) {
|
@@ -163,4 +163,53 @@ describe('error hook tests', () => {
|
163 | 163 | expect(result.error).toBe(undefined)
|
164 | 164 | })
|
165 | 165 | })
|
| 166 | + |
| 167 | + describe('error output suppression', () => { |
| 168 | + test('should allow console.error to be mocked', async () => { |
| 169 | + const consoleError = console.error |
| 170 | + console.error = jest.fn() |
| 171 | + |
| 172 | + try { |
| 173 | + const { hydrate, rerender, unmount } = renderHook( |
| 174 | + (stage) => { |
| 175 | + useEffect(() => { |
| 176 | + console.error(`expected in effect`) |
| 177 | + return () => { |
| 178 | + console.error(`expected in unmount`) |
| 179 | + } |
| 180 | + }, []) |
| 181 | + console.error(`expected in ${stage}`) |
| 182 | + }, |
| 183 | + { |
| 184 | + initialProps: 'render' |
| 185 | + } |
| 186 | + ) |
| 187 | + |
| 188 | + hydrate() |
| 189 | + |
| 190 | + act(() => { |
| 191 | + console.error('expected in act') |
| 192 | + }) |
| 193 | + |
| 194 | + await act(async () => { |
| 195 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 196 | + console.error('expected in async act') |
| 197 | + }) |
| 198 | + |
| 199 | + rerender('rerender') |
| 200 | + |
| 201 | + unmount() |
| 202 | + |
| 203 | + expect(console.error).toBeCalledWith('expected in render') // twice render/hydrate |
| 204 | + expect(console.error).toBeCalledWith('expected in effect') |
| 205 | + expect(console.error).toBeCalledWith('expected in act') |
| 206 | + expect(console.error).toBeCalledWith('expected in async act') |
| 207 | + expect(console.error).toBeCalledWith('expected in rerender') |
| 208 | + expect(console.error).toBeCalledWith('expected in unmount') |
| 209 | + expect(console.error).toBeCalledTimes(7) |
| 210 | + } finally { |
| 211 | + console.error = consoleError |
| 212 | + } |
| 213 | + }) |
| 214 | + }) |
166 | 215 | })
|
0 commit comments