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

Close comparison between float numbers within objects #3654

Closed
clarete opened this issue May 25, 2017 · 10 comments
Closed

Close comparison between float numbers within objects #3654

clarete opened this issue May 25, 2017 · 10 comments

Comments

@clarete
Copy link

clarete commented May 25, 2017

Do you want to request a feature or report a bug?

Feature request

What is the current behavior?

.toEqual doesn't provide close comparison between float numbers within objects. I'm aware of toBeCloseTo but it's just too much work for me to unpack all the sub (and sub-sub) properties within the object I want to test.

What is the expected behavior?

Maybe .toEqual could receive another parameter for the precision tolerated by tests, or maybe another explicit matcher like .toEqualWithFloats could be added.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

That's reproducible with the default jest configuration. I'm using jest v19.0.2.

Test case

test('compare floats within objects', () => {
  expect({ a: 0.30000000000000004 }).toEqualWithFloats({ a: 0.3 });
});
@thymikee
Copy link
Collaborator

Jest already can do that :). See http://facebook.github.io/jest/docs/en/expect.html#tobeclosetonumber-numdigits

@clarete
Copy link
Author

clarete commented May 26, 2017

@thymikee it's slightly different from my proposal. The link you provided only does that for a single value, I have an object with lots of properties that contain floats. unpacking each value and testing it would be really laborious. do you think it makes sense? I edited the description to be more specific. Thank you for the answer!

@thymikee
Copy link
Collaborator

I don't see the reason we should add this kind of matcher, as this use case is very specific.
And Jest also has expect.extend just for such occasions — to create your own matcher, useful inside your tests.

@clarete
Copy link
Author

clarete commented May 26, 2017

Fair enough. I just have tons of tables with floats because I'm building a UI for charts!

I was trying to reuse the jest tooling to create my own matcher via extend but I'd have to copy and paste code or write my own traversal function. I ended up just hardcoding some piece of data in my test so it always generates the same values and then toEqual becomes useful again.

Thanks for the response! <o/

@clawconduce
Copy link

See See https://github.com/maasencioh/jest-matcher-deep-close-to

@clawconduce
Copy link

Also see #4058

@clarete
Copy link
Author

clarete commented Jan 25, 2018

Nice! Thanks @clawconduce <o/

@garyo
Copy link

garyo commented Mar 1, 2021

See See https://github.com/maasencioh/jest-matcher-deep-close-to

That gets 3000 downloads per week now. Comparing things like three.js vectors and matrices doesn't seem like a "very specific" use case; on the contrary it seems pretty common in graphics. Couldn't this be added to Jest?

@wholebuzz
Copy link

++ because extending expect is horrible in typescript.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants