Skip to content

Commit

Permalink
Fix issue chaijs#166 - configurable threshold in objDisplay.
Browse files Browse the repository at this point in the history
  • Loading branch information
romario333 committed Nov 25, 2013
1 parent 55fb0e0 commit e07e57f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/chai/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ module.exports = function (_chai, util) {

Assertion.showDiff = true;

/*!
* ### Assertion.truncateThreshold
*
* User configurable property, sets length threshold for actual and
* expected values in assertion errors. If this threshold is exceeded,
* the value is truncated.
*
* Set it to zero if you want to disable truncating altogether.
*
* Assertion.truncateThreshold = 0; // disable truncating
*
* @api public
*/
Assertion.truncateThreshold = 40;

Assertion.addProperty = function (name, fn) {
util.addProperty(this.prototype, name, fn);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/chai/utils/objDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function (obj) {
var str = inspect(obj)
, type = Object.prototype.toString.call(obj);

if (str.length >= 40) {
if (chai.Assertion.truncateThreshold && str.length >= chai.Assertion.truncateThreshold) {
if (type === '[object Function]') {
return !obj.name || obj.name === ''
? '[Function]'
Expand Down
29 changes: 29 additions & 0 deletions test/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,33 @@ describe('configuration', function () {
}
}
});

describe('Assertion.truncateThreshold', function() {
var orig = chai.Assertion.truncateThreshold;

beforeEach(function() {
chai.Assertion.showDiff = false;
});

afterEach(function() {
chai.Assertion.truncateThreshold = orig;
chai.Assertion.showDiff = true;
});

it('is 20', function() {
chai.Assertion.truncateThreshold = 20;

err(function() {
assert.deepEqual({v: 'something longer than 20'}, {v: 'x'});
}, "expected { Object (v) } to deeply equal { v: 'x' }");
});

it('is 0', function() {
chai.Assertion.truncateThreshold = 0;

err(function() {
assert.deepEqual({v: 'something longer than 20'}, {v: 'x'});
}, "expected { v: 'something longer than 20' } to deeply equal { v: 'x' }");
});
});
});

0 comments on commit e07e57f

Please sign in to comment.