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

property assertion should only accept strings if nested, fixes #1043 #1044

Merged
merged 10 commits into from Oct 2, 2017
9 changes: 4 additions & 5 deletions test/assert.js
@@ -1,6 +1,5 @@
describe('assert', function () {
var assert = chai.assert;
var expect = chai.expect;

it('assert', function () {
var foo = 'bar';
Expand Down Expand Up @@ -1461,13 +1460,13 @@ describe('assert', function () {
assert.propertyVal(dummyObj, 'a', '2', 'blah');
}, "blah: expected { a: '1' } to have property 'a' of '2', but got '1'");

expect(function () {
chai.expect(function () {
assert.nestedProperty({a:1}, '{a:1}');
}).to.not.throw('the argument to `property` must be a string');
}).to.not.throw('the argument to property must be a string when using nested syntax');

expect(function () {
chai.expect(function () {
assert.nestedProperty({a:1}, {'a':'1'});
}).to.throw('the argument to `property` must be a string');
}).to.throw('the argument to property must be a string when using nested syntax');
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are using chai.expect on line 18 too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's shouldn't be wrong to use expect in this test, because here we are actually using chai to assert, we are not calling assertions and expecting them to work, we are testing that the assertion throws.

But I agree with @meeber that we must try and be able to use own interface to test it.

You can replace the ones you added on this PR with assert.throws and assert.doesNotThrows and maybe remove the others in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure @vieiralucas . I have replaced them and also surely will open a seperate PR in future to replace the remaining.

it('deepPropertyVal', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/expect.js
Expand Up @@ -1465,7 +1465,7 @@ describe('expect', function () {

expect(function () {
expect({a:1}).to.have.property(null);
}).to.throw('the argument to `property` must be either of type string, number or symbol');
}).to.throw('the argument to property must be a string, number, or symbol');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the err helper function, just like the test above it.

});

it('property(name, val)', function(){
Expand Down Expand Up @@ -1762,7 +1762,7 @@ describe('expect', function () {

expect(function () {
expect({a:1}).to.have.nested.property({'a':'1'});
}).to.throw('the argument to `property` must be a string');
}).to.throw('the argument to property must be a string when using nested syntax');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the err helper function.

});

it('nested.property(name, val)', function(){
Expand Down
9 changes: 4 additions & 5 deletions test/should.js
@@ -1,6 +1,5 @@
describe('should', function() {
var should = chai.Should();
var expect = chai.expect;

it('assertion', function(){
'test'.should.be.a('string');
Expand Down Expand Up @@ -1410,13 +1409,13 @@ describe('should', function() {

({a:1}).should.have.nested.property('a');

expect(function () {
(function () {
({a:1}).should.have.nested.property('{a:1}');
}).to.not.throw('the argument to `property` must be a string');
}).should.not.throw('the argument to property must be a string when using nested syntax');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this test. We already test that .nested.property works properly elsewhere in the suite. Adding this test with ".should.not.throw` doesn't add any value.


expect(function () {
(function () {
({a:1}).should.have.nested.property({'a':'1'});
}).to.throw('the argument to `property` must be a string');
}).should.throw('the argument to property must be a string when using nested syntax');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the err helper function, just like the test below it.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more test should be added here to the should interface tests in order to verify the error message "the argument to property must be a string, number, or symbol". This test can closely resemble the expect({a:1}).to.have.property(null); test that you added on the expect interface, except using the should interface this time instead of expect. (And using the err helper function).

err(function(){
({ 'foo.bar': 'baz' }).should.have.nested.property('foo.bar');
Expand Down