Skip to content

Instantly share code, notes, and snippets.

@beattyml1
Created March 5, 2018 23:18
Show Gist options
  • Save beattyml1/d0eee09fe6e78cf562c8b412cd867cb6 to your computer and use it in GitHub Desktop.
Save beattyml1/d0eee09fe6e78cf562c8b412cd867cb6 to your computer and use it in GitHub Desktop.
fixAsyncJestErrors
// 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