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:
committed by
Steve Gravrock
parent
1f23f1e4d2
commit
25816a6e77
67
spec/core/integration/CustomObjectFormatterSpec.js
Normal file
67
spec/core/integration/CustomObjectFormatterSpec.js
Normal file
@@ -0,0 +1,67 @@
|
||||
describe("Custom object formatters", function() {
|
||||
var env;
|
||||
|
||||
beforeEach(function() {
|
||||
env = new jasmineUnderTest.Env();
|
||||
env.configure({random: false});
|
||||
});
|
||||
|
||||
it("scopes custom object formatters to a spec", function(done) {
|
||||
env.it('a spec with custom pretty-printer', function() {
|
||||
env.addCustomObjectFormatter(function(obj) { return 'custom(' + obj + ')'; });
|
||||
env.expect(42).toBeUndefined();
|
||||
});
|
||||
|
||||
env.it('a spec without custom pretty-printer', function() {
|
||||
env.expect(42).toBeUndefined();
|
||||
});
|
||||
|
||||
var specResults = [];
|
||||
var specDone = function(result) {
|
||||
specResults.push(result);
|
||||
};
|
||||
var expectations = function() {
|
||||
expect(specResults[0].failedExpectations[0].message).toEqual("Expected custom(42) to be undefined.");
|
||||
expect(specResults[1].failedExpectations[0].message).toEqual("Expected 42 to be undefined.");
|
||||
done();
|
||||
};
|
||||
env.addReporter({ specDone:specDone, jasmineDone: expectations});
|
||||
|
||||
env.execute();
|
||||
});
|
||||
|
||||
it("scopes custom object formatters to a suite", function(done) {
|
||||
env.it('a spec without custom pretty-printer', function() {
|
||||
env.expect(42).toBeUndefined();
|
||||
});
|
||||
|
||||
env.describe('with custom pretty-printer', function() {
|
||||
env.beforeEach(function() {
|
||||
env.addCustomObjectFormatter(function(obj) { return 'custom(' + obj + ')'; });
|
||||
});
|
||||
|
||||
env.it('a spec', function() {
|
||||
env.expect(42).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
var specResults = [];
|
||||
var specDone = function(result) {
|
||||
specResults.push(result);
|
||||
};
|
||||
var expectations = function() {
|
||||
expect(specResults[0].failedExpectations[0].message).toEqual("Expected 42 to be undefined.");
|
||||
expect(specResults[1].failedExpectations[0].message).toEqual("Expected custom(42) to be undefined.");
|
||||
done();
|
||||
};
|
||||
env.addReporter({ specDone:specDone, jasmineDone: expectations});
|
||||
|
||||
env.execute();
|
||||
});
|
||||
|
||||
it("throws an exception if you try to add a custom object formatter outside a runable", function() {
|
||||
expect(function() {
|
||||
env.addCustomObjectFormatter(function() {});
|
||||
}).toThrowError('Custom object formatters must be added in a before function or a spec')
|
||||
});
|
||||
});
|
||||
@@ -48,6 +48,28 @@ describe('Matchers (Integration)', function() {
|
||||
});
|
||||
}
|
||||
|
||||
function verifyFailsWithCustomObjectFormatters(config) {
|
||||
it('uses custom object formatters', function(done) {
|
||||
var env = new jasmineUnderTest.Env();
|
||||
env.it('a spec', function () {
|
||||
env.addCustomObjectFormatter(config.formatter);
|
||||
config.expectations(env);
|
||||
});
|
||||
|
||||
var specExpectations = function (result) {
|
||||
expect(result.status).toEqual('failed');
|
||||
expect(result.failedExpectations.length)
|
||||
.withContext('Number of failed expectations')
|
||||
.toEqual(1);
|
||||
expect(result.failedExpectations[0].message)
|
||||
.toEqual(config.expectedMessage);
|
||||
};
|
||||
|
||||
env.addReporter({specDone: specExpectations, jasmineDone: done});
|
||||
env.execute();
|
||||
});
|
||||
}
|
||||
|
||||
function verifyPassesAsync(expectations) {
|
||||
it('passes', function(done) {
|
||||
jasmine.getEnv().requirePromises();
|
||||
@@ -101,6 +123,30 @@ describe('Matchers (Integration)', function() {
|
||||
});
|
||||
}
|
||||
|
||||
function verifyFailsWithCustomObjectFormattersAsync(config) {
|
||||
it('uses custom object formatters', function(done) {
|
||||
var env = new jasmineUnderTest.Env();
|
||||
jasmine.getEnv().requirePromises();
|
||||
env.it('a spec', function () {
|
||||
env.addCustomObjectFormatter(config.formatter);
|
||||
return config.expectations(env);
|
||||
});
|
||||
|
||||
var specExpectations = function (result) {
|
||||
expect(result.status).toEqual('failed');
|
||||
expect(result.failedExpectations.length)
|
||||
.withContext('Number of failed expectations')
|
||||
.toEqual(1);
|
||||
expect(result.failedExpectations[0].message)
|
||||
.toEqual(config.expectedMessage);
|
||||
};
|
||||
|
||||
env.addReporter({specDone: specExpectations, jasmineDone: done});
|
||||
env.execute();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
describe('nothing', function() {
|
||||
verifyPasses(function(env) {
|
||||
env.expect().nothing();
|
||||
@@ -217,6 +263,16 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(2).toBeNaN();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
env.expect(1).toBeNaN();
|
||||
},
|
||||
expectedMessage: 'Expected |1| to be NaN.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeNegativeInfinity', function() {
|
||||
@@ -227,6 +283,16 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(2).toBeNegativeInfinity();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
env.expect(1).toBeNegativeInfinity();
|
||||
},
|
||||
expectedMessage: 'Expected |1| to be -Infinity.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeNull', function() {
|
||||
@@ -247,6 +313,16 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(2).toBePositiveInfinity();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
env.expect(1).toBePositiveInfinity();
|
||||
},
|
||||
expectedMessage: 'Expected |1| to be Infinity.'
|
||||
})
|
||||
});
|
||||
|
||||
describe('toBeResolved', function() {
|
||||
@@ -270,6 +346,17 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFailsAsync(function(env) {
|
||||
return env.expectAsync(Promise.resolve('foo')).toBeResolvedTo('bar');
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormattersAsync({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
return env.expectAsync(Promise.resolve('x')).toBeResolvedTo('y');
|
||||
},
|
||||
expectedMessage: 'Expected a promise to be resolved to |y| ' +
|
||||
'but it was resolved to |x|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeRejected', function() {
|
||||
@@ -293,6 +380,17 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFailsAsync(function(env) {
|
||||
return env.expectAsync(Promise.resolve()).toBeRejectedWith('nope');
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormattersAsync({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
return env.expectAsync(Promise.reject('x')).toBeRejectedWith('y');
|
||||
},
|
||||
expectedMessage: 'Expected a promise to be rejected with |y| ' +
|
||||
'but it was rejected with |x|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeRejectedWithError', function() {
|
||||
@@ -303,6 +401,17 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFailsAsync(function(env) {
|
||||
return env.expectAsync(Promise.resolve()).toBeRejectedWithError(Error);
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormattersAsync({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
return env.expectAsync(Promise.reject('foo')).toBeRejectedWithError('foo');
|
||||
},
|
||||
expectedMessage: 'Expected a promise to be rejected with Error: |foo| ' +
|
||||
'but it was rejected with |foo|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeTrue', function() {
|
||||
@@ -359,6 +468,20 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect('a').toEqual('b');
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
if (val === 5) {
|
||||
return "five"
|
||||
} else if (val === 4) {
|
||||
return "four"
|
||||
}
|
||||
},
|
||||
expectations: function(env) {
|
||||
env.expect([{foo: 4}]).toEqual([{foo: 5}]);
|
||||
},
|
||||
expectedMessage: 'Expected $[0].foo = four to equal five.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toHaveBeenCalled', function() {
|
||||
@@ -417,6 +540,19 @@ describe('Matchers (Integration)', function() {
|
||||
var spy = env.createSpy();
|
||||
env.expect(spy).toHaveBeenCalledWith('foo');
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
var spy = env.createSpy('foo');
|
||||
env.expect(spy).toHaveBeenCalledWith('x');
|
||||
},
|
||||
expectedMessage: 'Expected spy foo to have been called with:\n' +
|
||||
' |x|\n' +
|
||||
'but it was never called.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toHaveClass', function() {
|
||||
@@ -458,6 +594,19 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(function() {}).toThrow();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
var spy = env.createSpy('foo');
|
||||
env.expect(function() {
|
||||
throw 'x'
|
||||
}).not.toThrow();
|
||||
},
|
||||
expectedMessage: 'Expected function not to throw, but it threw |x|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toThrowError', function() {
|
||||
@@ -468,6 +617,19 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(function() { }).toThrowError();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
var spy = env.createSpy('foo');
|
||||
env.expect(function() {
|
||||
throw 'x'
|
||||
}).toThrowError();
|
||||
},
|
||||
expectedMessage: 'Expected function to throw an Error, but it threw |x|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toThrowMatching', function() {
|
||||
@@ -482,5 +644,21 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(throws).toThrowMatching(function() { return false; });
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
var spy = env.createSpy('foo');
|
||||
env.expect(function() {
|
||||
throw new Error('nope')
|
||||
}).toThrowMatching(function() {
|
||||
return false;
|
||||
});
|
||||
},
|
||||
expectedMessage: 'Expected function to throw an exception matching ' +
|
||||
'a predicate, but it threw Error with message |nope|.'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user