Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check target's type in .property assertion #992

Merged
merged 1 commit into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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