From d1a57b4ecbb6cfe439a4bfccfe16eec34b2dd3dc Mon Sep 17 00:00:00 2001
From: Matt Phillips <matt@mattphillips.io>
Date: Tue, 7 Aug 2018 15:34:31 +0100
Subject: [PATCH] Extract wrap matcher logic into function

---
 src/withMessage.js | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/withMessage.js b/src/withMessage.js
index 7449310..ac33069 100644
--- a/src/withMessage.js
+++ b/src/withMessage.js
@@ -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 {