From 3ace4a0bbd14cb3fad45067c29fed9880936e3b7 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Tue, 3 Oct 2017 02:14:05 +0530 Subject: [PATCH] property assertion should only accept strings if nested, fixes #1043 (#1044) * property assertion should only accept strings if nested, fixes #1043 * similar logic seperated out * test cases for fix #1043 * type check if not isNested with tests * Tests for assert and should inteface * Error message change * changes in should and assert tests as per review * assert tests modified * Review comments changes * review comments fixes --- lib/chai/core/assertions.js | 25 ++++++++++++++++++++++--- test/assert.js | 13 +++++++++++++ test/expect.js | 8 ++++++++ test/should.js | 10 ++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 7b9090082..3145e1ba8 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -1762,10 +1762,30 @@ module.exports = function (chai, _) { , isOwn = flag(this, 'own') , flagMsg = flag(this, 'message') , obj = flag(this, 'object') - , ssfi = flag(this, 'ssfi'); + , ssfi = flag(this, 'ssfi') + , nameType = typeof name; + + flagMsg = flagMsg ? flagMsg + ': ' : ''; + + if (isNested) { + if (nameType !== 'string') { + throw new AssertionError( + flagMsg + 'the argument to property must be a string when using nested syntax', + undefined, + ssfi + ); + } + } else { + if (nameType !== 'string' && nameType !== 'number' && nameType !== 'symbol') { + throw new AssertionError( + flagMsg + 'the argument to property must be a string, number, or symbol', + undefined, + ssfi + ); + } + } if (isNested && isOwn) { - flagMsg = flagMsg ? flagMsg + ': ' : ''; throw new AssertionError( flagMsg + 'The "nested" and "own" flags cannot be combined.', undefined, @@ -1774,7 +1794,6 @@ module.exports = function (chai, _) { } if (obj === null || obj === undefined) { - flagMsg = flagMsg ? flagMsg + ': ' : ''; throw new AssertionError( flagMsg + 'Target cannot be null or undefined.', undefined, diff --git a/test/assert.js b/test/assert.js index 3282fd1dd..5197f2ef4 100644 --- a/test/assert.js +++ b/test/assert.js @@ -1393,6 +1393,7 @@ describe('assert', function () { var obj = { foo: { bar: 'baz' } }; var simpleObj = { foo: 'bar' }; var undefinedKeyObj = { foo: undefined }; + var dummyObj = { a: '1' }; assert.property(obj, 'foo'); assert.property(obj, 'toString'); assert.propertyVal(obj, 'toString', Object.prototype.toString); @@ -1452,6 +1453,18 @@ describe('assert', function () { err(function () { assert.property(undefined, 'a', 'blah'); }, "blah: Target cannot be null or undefined."); + + err(function () { + assert.property({a:1}, {'a':'1'}, 'blah'); + }, 'blah: the argument to property must be a string, number, or symbol'); + + err(function () { + assert.propertyVal(dummyObj, 'a', '2', 'blah'); + }, "blah: expected { a: '1' } to have property 'a' of '2', but got '1'"); + + err(function () { + assert.nestedProperty({a:1}, {'a':'1'}, 'blah'); + }, 'blah: the argument to property must be a string when using nested syntax'); }); it('deepPropertyVal', function () { diff --git a/test/expect.js b/test/expect.js index 0b5a0e5b2..bb4fcbfca 100644 --- a/test/expect.js +++ b/test/expect.js @@ -1462,6 +1462,10 @@ describe('expect', function () { err(function () { expect(undefined, 'blah').to.have.property("a"); }, "blah: Target cannot be null or undefined."); + + err(function () { + expect({a:1}, 'blah').to.have.property(null) + }, "blah: the argument to property must be a string, number, or symbol"); }); it('property(name, val)', function(){ @@ -1755,6 +1759,10 @@ describe('expect', function () { expect({ 'foo.bar': 'baz' }) .to.have.nested.property('foo.bar'); }, "expected { 'foo.bar': 'baz' } to have nested property 'foo.bar'"); + + err(function () { + expect({a:1}, 'blah').to.have.nested.property({'a':'1'}); + }, "blah: the argument to property must be a string when using nested syntax"); }); it('nested.property(name, val)', function(){ diff --git a/test/should.js b/test/should.js index ec3b947d9..2b62af318 100644 --- a/test/should.js +++ b/test/should.js @@ -1182,6 +1182,10 @@ describe('should', function() { err(function() { ({a: {b: 1}}).should.have.own.nested.property("a.b"); }, "The \"nested\" and \"own\" flags cannot be combined."); + + err(function () { + ({a:1}).should.have.property(undefined); + }, "the argument to property must be a string, number, or symbol"); }); it('property(name, val)', function(){ @@ -1407,6 +1411,12 @@ describe('should', function() { ({ 'foo.bar[]': 'baz'}).should.have.nested.property('foo\\.bar\\[\\]'); + ({a:1}).should.have.nested.property('a'); + + err(function(){ + ({a:1}).should.have.nested.property({'a':'1'}); + }, "the argument to property must be a string when using nested syntax"); + err(function(){ ({ 'foo.bar': 'baz' }).should.have.nested.property('foo.bar'); }, "expected { 'foo.bar': 'baz' } to have nested property 'foo.bar'");