Skip to content

Commit

Permalink
Merge pull request #992 from meeber/property-target-type
Browse files Browse the repository at this point in the history
fix: check target's type in `.property` assertion
  • Loading branch information
keithamus committed Jun 19, 2017
2 parents 616cf8b + e6ddf64 commit 351e968
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/chai/core/assertions.js
Expand Up @@ -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) {
Expand All @@ -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];

Expand Down
8 changes: 8 additions & 0 deletions test/assert.js
Expand Up @@ -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 () {
Expand Down
8 changes: 8 additions & 0 deletions test/expect.js
Expand Up @@ -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(){
Expand Down

0 comments on commit 351e968

Please sign in to comment.