From 63ed2b39485bb8d6ce3e5cf48516b1308cae8a64 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Wed, 4 Jun 2025 18:37:44 -0700 Subject: [PATCH] Include function names in pretty printer output This helps make matcher errors and spy strategy mismatch errors easier to understand in cases where the difference involves expecting one function but getting a different one. --- lib/jasmine-core/jasmine.js | 6 +++++- spec/core/PrettyPrintSpec.js | 15 +++++++++++++-- spec/core/matchers/toEqualSpec.js | 6 +++--- src/core/PrettyPrinter.js | 6 +++++- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 493d01c3..7d3e62a1 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -7761,7 +7761,11 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) { } else if (value instanceof RegExp) { this.emitScalar(value.toString()); } else if (typeof value === 'function') { - this.emitScalar('Function'); + if (value.name) { + this.emitScalar(`Function '${value.name}'`); + } else { + this.emitScalar('Function'); + } } else if (j$.isDomNode(value)) { if (value.tagName) { this.emitDomElement(value); diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index f49857bd..88194b56 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -164,7 +164,7 @@ describe('PrettyPrinter', function() { "Object({ foo: 'bar', baz: 3, nullValue: null, undefinedValue: undefined })" ); expect(pp({ foo: function() {}, bar: [1, 2, 3] })).toEqual( - 'Object({ foo: Function, bar: [ 1, 2, 3 ] })' + "Object({ foo: Function 'foo', bar: [ 1, 2, 3 ] })" ); }); @@ -450,7 +450,7 @@ describe('PrettyPrinter', function() { }; expect(pp(objFromOtherContext)).toEqual( - "Object({ foo: 'bar', toString: Function })" + "Object({ foo: 'bar', toString: Function 'toString' })" ); }); @@ -477,6 +477,17 @@ describe('PrettyPrinter', function() { expect(pp(a)).toEqual('({ })'); }); + it('stringifies functions with names', function() { + const pp = jasmineUnderTest.makePrettyPrinter(); + expect(pp(foo)).toEqual("Function 'foo'"); + function foo() {} + }); + + it('stringifies functions without names', function() { + const pp = jasmineUnderTest.makePrettyPrinter(); + expect(pp(function() {})).toEqual('Function'); + }); + it('should handle objects with null prototype', function() { const pp = jasmineUnderTest.makePrettyPrinter(); const obj = Object.create(null); diff --git a/spec/core/matchers/toEqualSpec.js b/spec/core/matchers/toEqualSpec.js index 55b5d9d9..16d24c71 100644 --- a/spec/core/matchers/toEqualSpec.js +++ b/spec/core/matchers/toEqualSpec.js @@ -458,9 +458,9 @@ describe('toEqual', function() { }); it('reports mismatches between Functions', function() { - const actual = { x: function() {} }, - expected = { x: function() {} }, - message = 'Expected $.x = Function to equal Function.'; + const actual = { x: function() {} }; + const expected = { x: function() {} }; + const message = "Expected $.x = Function 'x' to equal Function 'x'."; expect(compareEquals(actual, expected).message).toEqual(message); }); diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index 9a098f21..7ea2ffd8 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -35,7 +35,11 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) { } else if (value instanceof RegExp) { this.emitScalar(value.toString()); } else if (typeof value === 'function') { - this.emitScalar('Function'); + if (value.name) { + this.emitScalar(`Function '${value.name}'`); + } else { + this.emitScalar('Function'); + } } else if (j$.isDomNode(value)) { if (value.tagName) { this.emitDomElement(value);