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.
This commit is contained in:
Steve Gravrock
2025-06-04 18:37:44 -07:00
parent 0183acc682
commit 63ed2b3948
4 changed files with 26 additions and 7 deletions
+5 -1
View File
@@ -7761,7 +7761,11 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} else if (value instanceof RegExp) { } else if (value instanceof RegExp) {
this.emitScalar(value.toString()); this.emitScalar(value.toString());
} else if (typeof value === 'function') { } 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)) { } else if (j$.isDomNode(value)) {
if (value.tagName) { if (value.tagName) {
this.emitDomElement(value); this.emitDomElement(value);
+13 -2
View File
@@ -164,7 +164,7 @@ describe('PrettyPrinter', function() {
"Object({ foo: 'bar', baz: 3, nullValue: null, undefinedValue: undefined })" "Object({ foo: 'bar', baz: 3, nullValue: null, undefinedValue: undefined })"
); );
expect(pp({ foo: function() {}, bar: [1, 2, 3] })).toEqual( 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( 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('<anonymous>({ })'); expect(pp(a)).toEqual('<anonymous>({ })');
}); });
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() { it('should handle objects with null prototype', function() {
const pp = jasmineUnderTest.makePrettyPrinter(); const pp = jasmineUnderTest.makePrettyPrinter();
const obj = Object.create(null); const obj = Object.create(null);
+3 -3
View File
@@ -458,9 +458,9 @@ describe('toEqual', function() {
}); });
it('reports mismatches between Functions', function() { it('reports mismatches between Functions', function() {
const actual = { x: function() {} }, const actual = { x: function() {} };
expected = { x: function() {} }, const expected = { x: function() {} };
message = 'Expected $.x = Function to equal Function.'; const message = "Expected $.x = Function 'x' to equal Function 'x'.";
expect(compareEquals(actual, expected).message).toEqual(message); expect(compareEquals(actual, expected).message).toEqual(message);
}); });
+5 -1
View File
@@ -35,7 +35,11 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} else if (value instanceof RegExp) { } else if (value instanceof RegExp) {
this.emitScalar(value.toString()); this.emitScalar(value.toString());
} else if (typeof value === 'function') { } 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)) { } else if (j$.isDomNode(value)) {
if (value.tagName) { if (value.tagName) {
this.emitDomElement(value); this.emitDomElement(value);