diff --git a/common/stringify.js b/common/stringify.js index 327ec295a..8b07abc08 100644 --- a/common/stringify.js +++ b/common/stringify.js @@ -19,7 +19,18 @@ var stringify = function stringify (obj, depth) { case 'undefined': return 'undefined' case 'function': - return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }') + try { + // function abc(a, b, c) { /* code goes here */ } + // -> function abc(a, b, c) { ... } + return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }') + } catch (err) { + if (err instanceof TypeError) { + // Proxy(function abc(...) { ... }) + return 'Proxy(function ' + (obj.name || '') + '(...) { ... })' + } else { + throw err + } + } case 'boolean': return obj ? 'true' : 'false' case 'object': diff --git a/test/client/stringify.spec.js b/test/client/stringify.spec.js index 852bb74d4..241828295 100644 --- a/test/client/stringify.spec.js +++ b/test/client/stringify.spec.js @@ -36,6 +36,18 @@ describe('stringify', function () { }) }) + // Conditionally run Proxy tests as it's not supported by all browsers yet + // http://caniuse.com/#feat=proxy + if (window.Proxy) { + it('should serialize proxied functions', function () { + var abcProxy = new Proxy(function abc (a, b, c) { return 'whatever' }, {}) + var defProxy = new Proxy(function (d, e, f) { return 'whatever' }, {}) + + assert.deepEqual(stringify(abcProxy), 'Proxy(function abc(...) { ... })') + assert.deepEqual(stringify(defProxy), 'Proxy(function (...) { ... })') + }) + } + it('should serialize arrays', function () { assert.deepEqual(stringify(['a', 'b', null, true, false]), "['a', 'b', null, true, false]") })