Last active
October 21, 2019 21:05
-
-
Save aneurysmjs/fa90a40bc244461945d49739ae11444f to your computer and use it in GitHub Desktop.
handle promise exceptions inside a hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @link this is taken from here for reference | |
* https://github.com/testing-library/react-hooks-testing-library/issues/20#issuecomment-476628600 | |
*/ | |
import { useState, useEffect } from 'react'; | |
import { renderHook } from '@testing-library/react-hooks'; | |
describe('error hook tests', () => { | |
function useError(obj: Error | string): boolean { | |
if (obj.constructor.name === 'Error') { | |
// console.log('is a mudafocka err', err); | |
throw new Error(obj); | |
} | |
return obj; | |
} | |
const resolvedPromise = (): Promise<string> => Promise.resolve('good'); | |
const rejectedPromise = (): Promise<void> => import('/some/bad/file.js'); | |
function useAsyncError(fuckingPromise: () => Promise<any>): boolean { | |
const [value, setValue] = useState(); | |
useEffect(() => { | |
(async (): Promise<void> => { | |
try { | |
const result = await fuckingPromise(); | |
setValue(result); | |
} catch (err) { | |
console.log('is a mudafocka error', err); | |
setValue(err); | |
} | |
})(); | |
}, [fuckingPromise]); | |
return useError(value); | |
} | |
test('should raise async error', async () => { | |
const { result, waitForNextUpdate } = renderHook(() => useAsyncError(rejectedPromise)); | |
await waitForNextUpdate(); | |
expect(() => { | |
expect(result.current).not.toBe(undefined); | |
}).toThrow(Error(`Error: Cannot find module '/some/bad/file.js' from 'dummy.test.tsx'`)); | |
}); | |
test('should capture async error', async () => { | |
const { result, waitForNextUpdate } = renderHook(() => useAsyncError(rejectedPromise)); | |
await waitForNextUpdate(); | |
expect(result.error).toEqual( | |
Error(`Error: Cannot find module '/some/bad/file.js' from 'dummy.test.tsx'`), | |
); | |
}); | |
test('should not capture async error', async () => { | |
const { result, waitForNextUpdate } = renderHook(() => useAsyncError(resolvedPromise)); | |
await waitForNextUpdate(); | |
expect(result.current).not.toBe(undefined); | |
expect(result.error).toBe(undefined); | |
}); | |
test('should reset async error', async () => { | |
const { result, waitForNextUpdate, rerender } = renderHook( | |
prevPromise => useAsyncError(prevPromise), | |
{ | |
initialProps: rejectedPromise, | |
}, | |
); | |
await waitForNextUpdate(); | |
expect(result.error).not.toBe(undefined); | |
rerender(resolvedPromise); | |
await waitForNextUpdate(); | |
expect(result.current).not.toBe(undefined); | |
expect(result.error).toBe(undefined); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment