Created
March 5, 2018 23:18
-
-
Save beattyml1/d0eee09fe6e78cf562c8b412cd867cb6 to your computer and use it in GitHub Desktop.
fixAsyncJestErrors
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
// this doesn't work just yet but I'm trying to figure out the idea because async testing in jest is driving me nuts | |
async function fixAsyncErrors(action: (expect: (x) => Matchers) => Promise<any>|void) { | |
let errors = new Array(); | |
await (action(x => { | |
let matchers = expect(x); | |
let matcherProps = Object.getOwnPropertyNames(matchers); | |
let safeMatchers = matcherProps.reduce((safeMatchers, p) => { | |
let safe = typeof matchers[p] === 'function' ? (...args) => { | |
try { matchers[p](...args); } | |
catch (e) { errors.push(e); console.error(e) } | |
} : matchers[p]; | |
safeMatchers[p] = safe; | |
return safeMatchers; | |
}, {}) | |
return safeMatchers as Matchers; | |
}) as Promise<any>).catch(e => errors.push(e)) | |
return errors; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment