Skip to content

Commit

Permalink
General improvements on error messages and handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Salib committed Feb 25, 2015
1 parent 26e1c5d commit 0124dd5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 32 deletions.
32 changes: 18 additions & 14 deletions chai-shallow-deep-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
// null value
if (expect === null) {
if (! (actual === null)) {
throw 'Expected "' + actual +'" to be null at path "'+ path +'".';
throw 'Expected to have null but got "' + actual +'" at path "'+ path +'".';
}

return true;
}

// undefined value
// undefined expected value
if (typeof expect == 'undefined') {
if (! (typeof actual == 'undefined')) {
throw 'Expected "' + actual +'" to be undefined at path "'+ path +'".';
if (typeof actual != 'undefined') {
throw 'Expected to have undefined but got "' + actual +'" at path "'+ path +'".';
}

return true;
Expand All @@ -45,7 +45,7 @@
// scalar description
if (/boolean|number|string/.test(typeof expect)) {
if (expect != actual) {
throw 'Expected "' + actual +'" to equal "'+ expect +'" at path "'+ path +'".';
throw 'Expected to have "' + expect +'" but got "'+ actual +'" at path "'+ path +'".';
}

return true;
Expand All @@ -56,34 +56,38 @@
if (actual instanceof Date) {
if (expect.getTime() != actual.getTime()) {
throw(
'Expected "' + actual.toISOString() + '" to equal ' +
'"' + expect.toISOString() + '" at path "' + path + '".'
'Expected to have date "' + expect.toISOString() + '" but got ' +
'"' + actual.toISOString() + '" at path "' + path + '".'
);
}

} else {
throw(
'Expected "' + actual + '" to equal ' +
'"' + expect.toISOString() + '" at path "' + path + '".'
);
throw(
'Expected to have date "' + expect.toISOString() + '" but got ' +
'"' + actual + '" at path "' + path + '".'
);
}
}

if (actual === null) {
throw 'Expected null to be an array/object at path "' + path + '".';
throw 'Expected to have an array/object but got null at path "' + path + '".';
}

// array/object description
for (var prop in expect) {
shallowDeepEqual(expect[prop], actual[prop], path + '/' + prop);
if (typeof actual[prop] == 'undefined' && typeof expect[prop] != 'undefined') {
throw 'Expected "' + prop + '" field to be defined at path "' + path + '".';
}

shallowDeepEqual(expect[prop], actual[prop], path + (path == '/' ? '' : '/') + prop);
}

return true;
}

chai.Assertion.addMethod('shallowDeepEqual', function (expect) {
try {
shallowDeepEqual(expect, this._obj, '.');
shallowDeepEqual(expect, this._obj, '/');
}
catch (msg) {
this.assert(false, msg, undefined, expect, this._obj);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chai-shallow-deep-equal",
"version": "1.2.0",
"version": "1.3.0",
"description": "Shallow deep equal assertion for chai",
"keywords": [
"chai",
Expand All @@ -27,7 +27,7 @@
"chai": ">= 1.9.0"
},
"scripts": {
"test": "mocha --recursive -C"
"test": "mocha --recursive -C -R spec"
},
"main": "chai-shallow-deep-equal.js",
"directories": {
Expand Down
78 changes: 62 additions & 16 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ describe('chai-shallow-deep-equal', function() {
it('fail on scalar values', function() {
new chai.Assertion(function() {
new chai.Assertion(true).to.be.shallowDeepEqual(false);
}).fail('Expected "true" to equal "false" at path ".".');
}).fail('Expected to have "false" but got "true" at path "/".');

new chai.Assertion(function() {
new chai.Assertion(10).to.be.shallowDeepEqual(42);
}).fail('Expected "10" to equal "42" at path ".".');
}).fail('Expected to have "42" but got "10" at path "/".');

new chai.Assertion(function() {
new chai.Assertion('success').to.be.shallowDeepEqual('fail');
}).fail('Expected "success" to equal "fail" at path ".".');
}).fail('Expected to have "fail" but got "success" at path "/".');
});

it('success on empty objects', function() {
Expand All @@ -67,7 +67,7 @@ describe('chai-shallow-deep-equal', function() {
it('fail on simple objects', function() {
new chai.Assertion(function() {
new chai.Assertion({a: 10, b: 12}).to.be.shallowDeepEqual({a: 11});
}).fail('Expected "10" to equal "11" at path "./a".');
}).fail('Expected to have "11" but got "10" at path "/a".');
});

it('success on array', function() {
Expand All @@ -77,7 +77,7 @@ describe('chai-shallow-deep-equal', function() {
it('fail on array', function() {
new chai.Assertion(function() {
new chai.Assertion([10,11,12]).to.be.shallowDeepEqual([13]);
}).fail('Expected "10" to equal "13" at path "./0".');
}).fail('Expected to have "13" but got "10" at path "/0".');
});

it('success on deep objects', function() {
Expand All @@ -87,7 +87,7 @@ describe('chai-shallow-deep-equal', function() {
it('fail on deep objects', function() {
new chai.Assertion(function() {
new chai.Assertion({a: {b: 12, c: 15}}).to.be.shallowDeepEqual({a: {b: 13}});
}).fail('Expected "12" to equal "13" at path "./a/b".');
}).fail('Expected to have "13" but got "12" at path "/a/b".');
});

it('success on deep array', function() {
Expand All @@ -97,7 +97,7 @@ describe('chai-shallow-deep-equal', function() {
it('fail on deep array', function() {
new chai.Assertion(function() {
new chai.Assertion([{b: 12, c: 15}]).to.be.shallowDeepEqual([{b: 13}]);
}).fail('Expected "12" to equal "13" at path "./0/b".');
}).fail('Expected to have "13" but got "12" at path "/0/b".');
});

it('success on using object as array', function() {
Expand All @@ -107,7 +107,7 @@ describe('chai-shallow-deep-equal', function() {
it('fail on using object as array', function() {
new chai.Assertion(function() {
new chai.Assertion([{b: 12}, {c: 15}]).to.be.shallowDeepEqual({length: 3});
}).fail('Expected "2" to equal "3" at path "./length".');
}).fail('Expected to have "3" but got "2" at path "/length".');
});

it('success on accessors', function() {
Expand All @@ -131,8 +131,7 @@ describe('chai-shallow-deep-equal', function() {
new chai.Assertion(new Date('2014-09-30T20:00:00.000Z'))
.to.be.shallowDeepEqual(new Date('2014-09-29T20:00:00.000Z'));
}).fail(
'Expected "2014-09-30T20:00:00.000Z" to equal ' +
'"2014-09-29T20:00:00.000Z" at path ".".'
'Expected to have date "2014-09-29T20:00:00.000Z" but got "2014-09-30T20:00:00.000Z" at path "/".'
);
});

Expand All @@ -141,7 +140,7 @@ describe('chai-shallow-deep-equal', function() {
new chai.Assertion(42)
.to.be.shallowDeepEqual(new Date('2014-09-29T20:00:00.000Z'));
}).fail(
'Expected "42" to equal ' + '"2014-09-29T20:00:00.000Z" at path ".".'
'Expected to have date "2014-09-29T20:00:00.000Z" but got "42" at path "/".'
);
});

Expand All @@ -156,13 +155,13 @@ describe('chai-shallow-deep-equal', function() {
it('fail on missing properties', function() {
new chai.Assertion(function() {
new chai.Assertion({a: 10, b: 12}).to.be.shallowDeepEqual({a: 10, b: undefined});
}).fail('Expected "12" to be undefined at path "./b".');
}).fail('Expected to have undefined but got "12" at path "/b".');
});

it('fail on null properties', function() {
new chai.Assertion(function() {
new chai.Assertion({a: 10, b: 12}).to.be.shallowDeepEqual({a: 10, b: null});
}).fail('Expected "12" to be null at path "./b".');
}).fail('Expected to have null but got "12" at path "/b".');
});

it('success on null', function() {
Expand All @@ -174,22 +173,69 @@ describe('chai-shallow-deep-equal', function() {
new chai.Assertion(a.unknown).to.be.shallowDeepEqual(undefined);
});

it('success on undefined sub-field', function() {
var a = {};
new chai.Assertion(a).to.be.shallowDeepEqual({b: undefined});
});

it('fail on undefined sub-field', function() {
new chai.Assertion(function() {
var a = { b: null };
new chai.Assertion(a).to.be.shallowDeepEqual({b: undefined});
}).fail('Expected to have undefined but got "null" at path "/b".');
});

it('fail on undefined array sub-field', function() {
new chai.Assertion(function() {
var a = { };
new chai.Assertion(a).to.be.shallowDeepEqual({ b: [ ] });
}).fail('Expected "b" field to be defined at path "/".');
});

it('fail on undefined item in array', function() {
new chai.Assertion(function() {
var a = { b: [ ] };
new chai.Assertion(a).to.be.shallowDeepEqual({ b: [ { c: 'hello' } ] });
}).fail('Expected "0" field to be defined at path "/b".');
});

it('fail on null', function() {
new chai.Assertion(function() {
new chai.Assertion(23).to.be.shallowDeepEqual(null);
}).fail('Expected "23" to be null at path ".".');
}).fail('Expected to have null but got "23" at path "/".');
});

it('fail on undefined', function() {
new chai.Assertion(function() {
new chai.Assertion(23).to.be.shallowDeepEqual(undefined);
}).fail('Expected "23" to be undefined at path ".".');
}).fail('Expected to have undefined but got "23" at path "/".');
});

it('fail on null sub-field', function() {
new chai.Assertion(function() {
var a = { b: 'abc' };
new chai.Assertion(a).to.be.shallowDeepEqual({b: null});
}).fail('Expected to have null but got "abc" at path "/b".');
});

it('fail on null array sub-field', function() {
new chai.Assertion(function() {
var a = { b: null };
new chai.Assertion(a).to.be.shallowDeepEqual({ b: [ ] });
}).fail('Expected to have an array/object but got null at path "/b".');
});

it('fail on null item in array', function() {
new chai.Assertion(function() {
var a = { b: [ null ] };
new chai.Assertion(a).to.be.shallowDeepEqual({ b: [ { c: 'hello' } ] });
}).fail('Expected to have an array/object but got null at path "/b/0".');
});

it('fail on unexisting array', function() {
new chai.Assertion(function() {
new chai.Assertion(null).to.be.shallowDeepEqual(['a']);
}).fail('Expected null to be an array/object at path ".".');
}).fail('Expected to have an array/object but got null at path "/".');
});

});

0 comments on commit 0124dd5

Please sign in to comment.