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

should.approximately support #525

Closed
axelpale opened this issue Oct 2, 2015 · 5 comments
Closed

should.approximately support #525

axelpale opened this issue Oct 2, 2015 · 5 comments

Comments

@axelpale
Copy link

axelpale commented Oct 2, 2015

I propose ShouldJS's approximately(value, delta, [description]) assertion should be a part of the core API of Chai. One could use .within(above, below) to do the job but writing the both limits easily causes code similar to:

var epsilon = 0.01;
...
var val = 10;
(9.99).should.be.within(val - epsilon, val + epsilon);

versus drier:

var epsilon = 0.01;
...
`(9.99).should.be.approximately(10, epsilon);`

Furthermore because Chai claims to have Should interface, having all ShouldJS's methods is expected. Therefore to have a separate Chai plugin does not feel practical.

@axelpale
Copy link
Author

axelpale commented Oct 2, 2015

To underline the inconvenience, in my case (PhantomJS + Mocha + Chai + chai-jquery) going back to original ShouldJS instead of Chai is not an option because the benefits from chai-jquery.

@keithamus
Copy link
Member

Hey @axelpale thanks for the issue!

We have .closeTo which I think it what you're after, to quote the docs:

.closeTo(expected, delta)

  • @param{ Number }expected
  • @param{ Number }delta
  • @param{ String }message_optional_
    Asserts that the target is equal expected, to within a +/- delta range.
expect(1.5).to.be.closeTo(1, 0.5);

If you'd like, I'll happily accept a PR which aliases approximately to closeTo. Heres the code for .closeTo, and above that is an example of how we create multiple aliases for a method. Simply refactor the closeTo code to use the alias syntax, and add some new tests for the alias, and I'll merge 😄

@berkerpeksag
Copy link

The alias is added in 7fe23e9. This can be closed now.

@keithamus
Copy link
Member

👍

@axelpale
Copy link
Author

axelpale commented Oct 7, 2015

Oh, nice! Thank you guys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants