Added support for custom object formatters

Custom object formatters allow users to customize how an object is
stringified in matcher failure messages. This can already be done by
adding a `jasmineToString` method to the objects in question. But
it's not always desirable or possible to do that, particularly when
objects of a given "type" do not inherit from a specific prototype.
For instance, suppose a web service returns a list of foos that are
deserialized from JSON, e.g.:

   { fooId: 42, /* more properties */ }

The only way to define `jasmineToString` on those is by writing code to
add it to each instance at runtime. But a custom object formatter can
recognize that the object it's looking at is a foo and format it
accordingly:

   jasmine.addCustomObjectFormatter(function(obj) {
      if (typeof obj.fooId !== 'number') {
            return undefined;
        }

        return '[Foo with ID ' + obj.fooId + ']';
    });

Unlike `jasmineToString`, custom object formatters are scoped to a
particular spec or suite and don't require any changes to the code
under test.
This commit is contained in:
Steve Gravrock
2020-01-11 14:51:12 -08:00
committed by Steve Gravrock
parent 1f23f1e4d2
commit 25816a6e77
25 changed files with 591 additions and 73 deletions

View File

@@ -42,9 +42,14 @@ describe("ArrayContaining", function() {
});
it("jasmineToStrings itself", function() {
var containing = new jasmineUnderTest.ArrayContaining([]);
var sample = [],
matcher = new jasmineUnderTest.ArrayContaining(sample),
pp = jasmine.createSpy('pp').and.returnValue('sample');
expect(containing.jasmineToString()).toMatch("<jasmine.arrayContaining");
expect(matcher.jasmineToString(pp)).toEqual(
'<jasmine.arrayContaining(sample)>'
);
expect(pp).toHaveBeenCalledWith(sample);
});
it("uses custom equality testers", function() {

View File

@@ -30,9 +30,14 @@ describe("ArrayWithExactContents", function() {
});
it("jasmineToStrings itself", function() {
var matcher = new jasmineUnderTest.ArrayWithExactContents([]);
var sample = [],
matcher = new jasmineUnderTest.ArrayWithExactContents(sample),
pp = jasmine.createSpy('pp').and.returnValue('sample');
expect(matcher.jasmineToString()).toMatch("<jasmine.arrayWithExactContents");
expect(matcher.jasmineToString(pp)).toEqual(
'<jasmine.arrayWithExactContents(sample)>'
);
expect(pp).toHaveBeenCalledWith(sample);
});
it("uses custom equality testers", function() {

View File

@@ -184,8 +184,13 @@ describe('MapContaining', function() {
});
it('defines a `jasmineToString` method', function() {
var containing = new jasmineUnderTest.MapContaining(new Map());
var sample = new Map(),
containing = new jasmineUnderTest.MapContaining(sample),
pp = jasmine.createSpy('pp').and.returnValue('sample');
expect(containing.jasmineToString()).toMatch(/^<jasmine\.mapContaining/);
expect(containing.jasmineToString(pp)).toEqual(
'<jasmine.mapContaining(sample)>'
);
expect(pp).toHaveBeenCalledWith(sample);
});
});

View File

@@ -37,9 +37,15 @@ describe("ObjectContaining", function() {
});
it("jasmineToString's itself", function() {
var containing = new jasmineUnderTest.ObjectContaining({});
var sample = {},
matcher = new jasmineUnderTest.ObjectContaining(sample),
pp = jasmine.createSpy('pp').and.returnValue('sample');
expect(matcher.jasmineToString(pp)).toEqual(
'<jasmine.objectContaining(sample)>'
);
expect(pp).toHaveBeenCalledWith(sample);
expect(containing.jasmineToString()).toMatch("<jasmine.objectContaining");
});
it("matches recursively", function() {

View File

@@ -117,8 +117,13 @@ describe('SetContaining', function() {
});
it('defines a `jasmineToString` method', function() {
var containing = new jasmineUnderTest.SetContaining(new Set());
var sample = new Set(),
containing = new jasmineUnderTest.SetContaining(sample),
pp = jasmine.createSpy('pp').and.returnValue('sample');
expect(containing.jasmineToString()).toMatch(/^<jasmine\.setContaining/);
expect(containing.jasmineToString(pp)).toEqual(
'<jasmine.setContaining(sample)>'
);
expect(pp).toHaveBeenCalledWith(sample);
});
});