| 
 | 1 | +import { useEffect } from 'react'  | 
 | 2 | + | 
 | 3 | +import { ReactHooksRenderer } from '../../types/react'  | 
 | 4 | + | 
 | 5 | +describe('error output suppression tests', () => {  | 
 | 6 | +  test('should not suppress relevant errors', () => {  | 
 | 7 | +    const consoleError = console.error  | 
 | 8 | +    console.error = jest.fn()  | 
 | 9 | + | 
 | 10 | +    const { suppressErrorOutput } = require('..') as ReactHooksRenderer  | 
 | 11 | + | 
 | 12 | +    try {  | 
 | 13 | +      const restoreConsole = suppressErrorOutput()  | 
 | 14 | + | 
 | 15 | +      console.error('expected')  | 
 | 16 | +      console.error(new Error('expected'))  | 
 | 17 | +      console.error('expected with args', new Error('expected'))  | 
 | 18 | + | 
 | 19 | +      restoreConsole()  | 
 | 20 | + | 
 | 21 | +      expect(console.error).toBeCalledWith('expected')  | 
 | 22 | +      expect(console.error).toBeCalledWith(new Error('expected'))  | 
 | 23 | +      expect(console.error).toBeCalledWith('expected with args', new Error('expected'))  | 
 | 24 | +      expect(console.error).toBeCalledTimes(3)  | 
 | 25 | +    } finally {  | 
 | 26 | +      console.error = consoleError  | 
 | 27 | +    }  | 
 | 28 | +  })  | 
 | 29 | + | 
 | 30 | +  test('should allow console.error to be mocked', async () => {  | 
 | 31 | +    const { renderHook, act } = require('..') as ReactHooksRenderer  | 
 | 32 | +    const consoleError = console.error  | 
 | 33 | +    console.error = jest.fn()  | 
 | 34 | + | 
 | 35 | +    try {  | 
 | 36 | +      const { rerender, unmount } = renderHook(  | 
 | 37 | +        (stage) => {  | 
 | 38 | +          useEffect(() => {  | 
 | 39 | +            console.error(`expected in effect`)  | 
 | 40 | +            return () => {  | 
 | 41 | +              console.error(`expected in unmount`)  | 
 | 42 | +            }  | 
 | 43 | +          }, [])  | 
 | 44 | +          console.error(`expected in ${stage}`)  | 
 | 45 | +        },  | 
 | 46 | +        {  | 
 | 47 | +          initialProps: 'render'  | 
 | 48 | +        }  | 
 | 49 | +      )  | 
 | 50 | + | 
 | 51 | +      act(() => {  | 
 | 52 | +        console.error('expected in act')  | 
 | 53 | +      })  | 
 | 54 | + | 
 | 55 | +      await act(async () => {  | 
 | 56 | +        await new Promise((resolve) => setTimeout(resolve, 100))  | 
 | 57 | +        console.error('expected in async act')  | 
 | 58 | +      })  | 
 | 59 | + | 
 | 60 | +      rerender('rerender')  | 
 | 61 | + | 
 | 62 | +      unmount()  | 
 | 63 | + | 
 | 64 | +      expect(console.error).toBeCalledWith('expected in render')  | 
 | 65 | +      expect(console.error).toBeCalledWith('expected in effect')  | 
 | 66 | +      expect(console.error).toBeCalledWith('expected in act')  | 
 | 67 | +      expect(console.error).toBeCalledWith('expected in async act')  | 
 | 68 | +      expect(console.error).toBeCalledWith('expected in rerender')  | 
 | 69 | +      expect(console.error).toBeCalledWith('expected in unmount')  | 
 | 70 | +      expect(console.error).toBeCalledTimes(6)  | 
 | 71 | +    } finally {  | 
 | 72 | +      console.error = consoleError  | 
 | 73 | +    }  | 
 | 74 | +  })  | 
 | 75 | +})  | 
0 commit comments