Repository: unexpectedjs/unexpected
Pull request #408 (2017-07-15 14:28:26)

Fix inspection of bound functions (broken in 4485bf622 / 10.30.0)


Patch:
diff --git a/lib/types.js b/lib/types.js
index 76fc0ab1..c7d5f000 100644
--- a/lib/types.js
+++ b/lib/types.js
@@ -768,6 +768,10 @@ module.exports = function (expect) {
             var matchSource = source.match(/^\s*((?:async )?\s*(?:\S+\s*=>|\([^\)]*\)\s*=>|function \w*?\s*\([^\)]*\)))([\s\S]*)$/);
             if (matchSource) {
                 preamble = matchSource[1];
+                if (preamble === 'function ()' && name) {
+                    // fn.bind() doesn't seem to include the name in the .toString() output:
+                    preamble = 'function ' + name + '()';
+                }
                 body = matchSource[2];
                 var matchBodyAndIndent = body.match(/^(\s*\{)([\s\S]*?)([ ]*)\}\s*$/);
                 var openingBrace;


Test patch:
diff --git a/test/types/function-type.spec.js b/test/types/function-type.spec.js
index 18ee5b99..a2566c88 100644
--- a/test/types/function-type.spec.js
+++ b/test/types/function-type.spec.js
@@ -1,16 +1,49 @@
 /*global expect*/
 describe('function type', function () {
-    it('should inspect an empty function correctly', function () {
-        expect(function () {}, 'to inspect as',
-               'function () {}'
-              );
+    it('should inspect an empty anonymous function correctly', function () {
+        expect(
+            function () {},
+            'to inspect as',
+            'function () {}'
+        );
+    });
+
+    it('should inspect an empty named function correctly', function () {
+        expect(
+            function foo() {},
+            'to inspect as',
+            'function foo() {}'
+        );
     });
 
+    var isNodeJs3OrBelow = typeof process === 'object' && /^v[0123]\./.test(process.version);
+    var isPhantomJs = typeof navigator !== 'undefined' && /phantom/i.test(navigator.userAgent);
+    if (!isNodeJs3OrBelow && !isPhantomJs) {
+        // Node.js 3 and below and phantom.js don't include "bound " and the
+        // old Phantom.js version that's currently available on Travis doesn't
+        // even have Function#bind, which we then polyfill via es5-shim, leading
+        // to even more different output.
+        // For now let's just disable these tests in those environments
+
+        it('should inspect an anonymous bound function correctly', function () {
+            expect(
+                function () {}.bind({}),
+                'to inspect as',
+                'function bound () { /* native code */ }'
+             );
+        });
+
+        it('should inspect a named bound function correctly', function () {
+            expect(
+                function foo() {}.bind({}),
+                'to inspect as',
+                'function bound foo() { /* native code */ }'
+            );
+        });
+    }
+
     it('should inspect an function with just a newline correctly', function () {
-        expect(function () {
-        }, 'to inspect as',
-               'function () {}'
-              );
+        expect(function () {}, 'to inspect as', 'function () {}');
     });
 
     it('should inspect a one-line function correctly', function () {