fix: closeTo should check value's type before assertion #279

Merged
merged 1 commit into from Sep 29, 2014

Projects

None yet

3 participants

@mohayonao
Contributor

Assertion#closeTo passes tests that should be failed.

e.g.

expect([1.5]).to.be.closeTo(1.0, 0.5); // PASS???
expect(1.5).to.be.closeTo("1.0", 0.5); // PASS???

so, I fixed it.

expect([1.5]).to.be.closeTo(1.0, 0.5); // throw "expected [ 1.5 ] to be a number"
expect(1.5).to.be.closeTo("1.0", 0.5); // throw "expected '1.0' to be a number"
@charlierudolph
Contributor

👍 Definitely a useful error message.

I think the message should be different though when the supplied arguments to closeTo are wrong versus the actual value. Should probably verify delta is a number too.

expect([1.5]).to.be.closeTo(1.0, 0.5); // throw "expected [ 1.5 ] to be a number"
expect(1.5).to.be.closeTo("1.0", 0.5); // throw "the arguments to closeTo must be numbers"
expect(1.5).to.be.closeTo(1.0, "0.5"); // throw "the arguments to closeTo must be numbers"
@mohayonao
Contributor

I agree.
I've fixed this commit to throw understandable error messages.
Thanks!

expect([1.5]).to.be.closeTo(1.0, 0.5); // throw "expected [ 1.5 ] to be a number"
expect(1.5).to.be.closeTo("1.0", 0.5); // throw "the arguments to closeTo must be numbers"
expect(1.5).to.be.closeTo(1.0, "0.5"); // throw "the arguments to closeTo must be numbers"
@logicalparadox logicalparadox merged commit 3e692e6 into chaijs:master Sep 29, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment