Merge pull request #992 from meeber/property-target-type

fix: check target's type in `.property` assertion
This commit is contained in:
Keith Cirkel 2017-06-19 10:47:41 +01:00 committed by GitHub
commit 351e9689d3
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(){