diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d40d567e3c..0d534db5cf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Added small version of `EuiCallOut` [(#269)](https://github.com/elastic/eui/pull/269) - Added first batch of TypeScript type definitions for components and services [(#252)](https://github.com/elastic/eui/pull/252) - Added button for expanding `` instances to be full-screen. [(#259)](https://github.com/elastic/eui/pull/259) +- Add test helper for async functions that throw exceptions [#301](https://github.com/elastic/eui/pull/301) **Bug fixes** diff --git a/src/test/index.js b/src/test/index.js index af1224e7b7b..616df81665c 100644 --- a/src/test/index.js +++ b/src/test/index.js @@ -2,3 +2,4 @@ export { requiredProps } from './required_props'; export { takeMountedSnapshot } from './take_mounted_snapshot'; export { findTestSubject } from './find_test_subject'; export { startThrowingReactWarnings, stopThrowingReactWarnings } from './react_warnings'; +export { syncify } from './syncify'; diff --git a/src/test/syncify.js b/src/test/syncify.js new file mode 100644 index 00000000000..ec4fbeff990 --- /dev/null +++ b/src/test/syncify.js @@ -0,0 +1,13 @@ +// Helper designed to help test async/await functions that throw exceptions +// Example usage: +// const fn = await syncify(() => asyncFn()); +// expect(fn).toThrow(); +// https://github.com/facebook/jest/issues/1377 +export const syncify = async (fn) => { + try { + const result = await fn(); + return () => { return result; }; + } catch (e) { + return () => { throw e; }; + } +};