fix: check target's type in .property assertion

Previously, the `.property` assertion failed ungracefully if the target was `null` or `undefined`. This commit causes an `AssertionError` to be thrown instead so that the ssfi flag and custom error messages are respected.
This commit is contained in:
Grant Snodgrass 2017-06-10 17:51:50 -04:00
parent 616cf8bf88
commit e6ddf64c8f
3 changed files with 26 additions and 1 deletions

View file

@ -1644,6 +1644,7 @@ module.exports = function (chai, _) {
var isNested = flag(this, 'nested')
, isOwn = flag(this, 'own')
, flagMsg = flag(this, 'message')
, obj = flag(this, 'object')
, ssfi = flag(this, 'ssfi');
if (isNested && isOwn) {
@ -1655,9 +1656,17 @@ module.exports = function (chai, _) {
);
}
if (obj === null || obj === undefined) {
flagMsg = flagMsg ? flagMsg + ': ' : '';
throw new AssertionError(
flagMsg + 'Target cannot be null or undefined.',
undefined,
ssfi
);
}
var isDeep = flag(this, 'deep')
, negate = flag(this, 'negate')
, obj = flag(this, 'object')
, pathInfo = isNested ? _.getPathInfo(obj, name) : null
, value = isNested ? pathInfo.value : obj[name];

View file

@ -1339,6 +1339,14 @@ describe('assert', function () {
err(function () {
assert.notNestedPropertyVal(obj, 'foo.bar', 'baz', 'blah');
}, "blah: expected { foo: { bar: 'baz' } } to not have nested property 'foo.bar' of 'baz'");
err(function () {
assert.property(null, 'a', 'blah');
}, "blah: Target cannot be null or undefined.");
err(function () {
assert.property(undefined, 'a', 'blah');
}, "blah: Target cannot be null or undefined.");
});
it('deepPropertyVal', function () {

View file

@ -1263,6 +1263,14 @@ describe('expect', function () {
err(function() {
expect({a: {b: 1}}, 'blah').to.have.own.nested.property("a.b");
}, "blah: The \"nested\" and \"own\" flags cannot be combined.");
err(function () {
expect(null, 'blah').to.have.property("a");
}, "blah: Target cannot be null or undefined.");
err(function () {
expect(undefined, 'blah').to.have.property("a");
}, "blah: Target cannot be null or undefined.");
});
it('property(name, val)', function(){