Skip to content

Commit

Permalink
Added effect error test suite for #308 (ignored for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeyper committed May 4, 2020
1 parent 5238bad commit a591f1a
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions test/errorHook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ describe('error hook tests', () => {
return true
}

const somePromise = () => Promise.resolve()

function useAsyncError(throwError) {
const [value, setValue] = useState()
useEffect(() => {
somePromise().then(() => {
setValue(throwError)
})
const timeout = setTimeout(() => setValue(throwError), 100)
return () => clearTimeout(timeout)
}, [throwError])
return useError(value)
}

function useEffectError(throwError) {
useEffect(() => {
useError(throwError)
}, [])
return true
}

describe('synchronous', () => {
test('should raise error', () => {
const { result } = renderHook(() => useError(true))
Expand Down Expand Up @@ -105,4 +109,47 @@ describe('error hook tests', () => {
expect(result.error).toBe(undefined)
})
})

/*
These tests capture error cases that are not currently being caught successfully.
Refer to https://github.com/testing-library/react-hooks-testing-library/issues/308
for more details.
*/
describe.skip('effect', () => {
test('should raise effect error', () => {
const { result } = renderHook(() => useEffectError(true))

expect(() => {
expect(result.current).not.toBe(undefined)
}).toThrow(Error('expected'))
})

test('should capture effect error', () => {
const { result } = renderHook(() => useEffectError(true))
expect(result.error).toEqual(Error('expected'))
})

test('should not capture effect error', () => {
const { result } = renderHook(() => useEffectError(false))

expect(result.current).not.toBe(undefined)
expect(result.error).toBe(undefined)
})

test('should reset effect error', () => {
const { result, waitForNextUpdate, rerender } = renderHook(
(throwError) => useEffectError(throwError),
{
initialProps: true
}
)

expect(result.error).not.toBe(undefined)

rerender(false)

expect(result.current).not.toBe(undefined)
expect(result.error).toBe(undefined)
})
})
})

0 comments on commit a591f1a

Please sign in to comment.