Skip to content

Commit a591f1a

Browse files
committed
Added effect error test suite for #308 (ignored for now)
1 parent 5238bad commit a591f1a

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

test/errorHook.test.js

+52-5
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ describe('error hook tests', () => {
99
return true
1010
}
1111

12-
const somePromise = () => Promise.resolve()
13-
1412
function useAsyncError(throwError) {
1513
const [value, setValue] = useState()
1614
useEffect(() => {
17-
somePromise().then(() => {
18-
setValue(throwError)
19-
})
15+
const timeout = setTimeout(() => setValue(throwError), 100)
16+
return () => clearTimeout(timeout)
2017
}, [throwError])
2118
return useError(value)
2219
}
2320

21+
function useEffectError(throwError) {
22+
useEffect(() => {
23+
useError(throwError)
24+
}, [])
25+
return true
26+
}
27+
2428
describe('synchronous', () => {
2529
test('should raise error', () => {
2630
const { result } = renderHook(() => useError(true))
@@ -105,4 +109,47 @@ describe('error hook tests', () => {
105109
expect(result.error).toBe(undefined)
106110
})
107111
})
112+
113+
/*
114+
These tests capture error cases that are not currently being caught successfully.
115+
Refer to https://github.com/testing-library/react-hooks-testing-library/issues/308
116+
for more details.
117+
*/
118+
describe.skip('effect', () => {
119+
test('should raise effect error', () => {
120+
const { result } = renderHook(() => useEffectError(true))
121+
122+
expect(() => {
123+
expect(result.current).not.toBe(undefined)
124+
}).toThrow(Error('expected'))
125+
})
126+
127+
test('should capture effect error', () => {
128+
const { result } = renderHook(() => useEffectError(true))
129+
expect(result.error).toEqual(Error('expected'))
130+
})
131+
132+
test('should not capture effect error', () => {
133+
const { result } = renderHook(() => useEffectError(false))
134+
135+
expect(result.current).not.toBe(undefined)
136+
expect(result.error).toBe(undefined)
137+
})
138+
139+
test('should reset effect error', () => {
140+
const { result, waitForNextUpdate, rerender } = renderHook(
141+
(throwError) => useEffectError(throwError),
142+
{
143+
initialProps: true
144+
}
145+
)
146+
147+
expect(result.error).not.toBe(undefined)
148+
149+
rerender(false)
150+
151+
expect(result.current).not.toBe(undefined)
152+
expect(result.error).toBe(undefined)
153+
})
154+
})
108155
})

0 commit comments

Comments
 (0)