Skip to content

Commit

Permalink
Extract wrap matcher logic into function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Aug 7, 2018
1 parent 0fd13a0 commit d1a57b4
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/withMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,33 @@ class JestAssertionError extends Error {
}
}

const wrapMatcher = (matcher, customMessage) => {
const newMatcher = (...args) => {
try {
matcher(...args);
} catch (error) {
if (typeof customMessage !== 'string' || customMessage.length < 1 || !error.matcherResult) {
throw error;
}

const { matcherResult } = error;
const message = () => 'Custom message:\n ' + customMessage + '\n\n' + matcherResult.message();

throw new JestAssertionError({ ...matcherResult, message }, newMatcher);
}
};
return newMatcher;
};

const wrapMatchers = (matchers, customMessage) => {
return Object.keys(matchers).reduce((acc, name) => {
const matcher = matchers[name];

if (typeof matcher === 'function') {
const newMatcher = (...args) => {
try {
matcher(...args);
} catch (error) {
if (typeof customMessage !== 'string' || customMessage.length < 1 || !error.matcherResult) {
throw error;
}

const { matcherResult } = error;
const message = () => 'Custom message:\n ' + customMessage + '\n\n' + matcherResult.message();

throw new JestAssertionError(Object.assign({}, matcherResult, { message }), newMatcher);
}
return {
...acc,
[name]: wrapMatcher(matcher, customMessage)
};
return { ...acc, [name]: newMatcher };
}

return {
Expand Down

0 comments on commit d1a57b4

Please sign in to comment.