From 969cf7c8617e94b25b201aeb9e84064815cc2f3a Mon Sep 17 00:00:00 2001 From: Chris Roberson Date: Fri, 12 Jan 2018 12:34:47 -0500 Subject: [PATCH] Add syncify helper for testing async functions that throw exceptions --- src/test/index.js | 1 + src/test/syncify.js | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 src/test/syncify.js diff --git a/src/test/index.js b/src/test/index.js index af1224e7b7b3..616df81665cb 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 000000000000..e28f0f1d6f1b --- /dev/null +++ b/src/test/syncify.js @@ -0,0 +1,10 @@ +// Helper designed to help test async/await functions that throw exceptions +// 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; }; + } +};