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

assert: handle NaN properly in deep assertions #2440

Closed
wants to merge 1 commit into from

Conversation

thefourtheye
Copy link
Contributor

As it is, when two container objects which have NaNs are asserted
to be equal, it will fail.

> process.version
'v3.0.0'
> assert.deepEqual([NaN], [NaN])
...
AssertionError: [ NaN ] deepEqual [ NaN ]
> assert.deepEqual({key: NaN}, {key: NaN})
...

This patch explicitly compares the NaN values with Number.isNaN.

PR-URL: #2440

@thefourtheye thefourtheye added the assert Issues and PRs related to the assert subsystem. label Aug 19, 2015
As it is, when two container objects which have `NaN`s are asserted
to be equal, it will fail.

    > process.version
    'v3.0.0'
    > assert.deepEqual([NaN], [NaN])
    ...
    AssertionError: [ NaN ] deepEqual [ NaN ]
    > assert.deepEqual({key: NaN}, {key: NaN})
    ...

This patch explicitly compares the `NaN` values with `Number.isNaN`.

PR-URL: nodejs#2440
@targos
Copy link
Member

targos commented Aug 19, 2015

I'm not sure about this... NaN usually appears because of a programmer mistake. Do you have a use case where it should really be expected in a result ?

@vkurchatkin
Copy link
Contributor

NaN is really not equal to NaN any language, it is a part of ieee 754 standard. Changing that would be confusing.

@thefourtheye
Copy link
Contributor Author

@targos I don't have a specific use case. But it is one of the special cases in the language and as it is we don't handle it well, IMHO.

@vkurchatkin Agreed, but I believe assertion module is not necessarily just a comparison module. When I am writing a test, lets say, I am expecting a function to return an array of NaNs. And when I use assert.deepEqual family of functions to check if my expectation is correct, it would say "NO" as it is.

@brendanashworth brendanashworth added the semver-major PRs that contain breaking changes and should be released in the next major version. label Aug 19, 2015
@brendanashworth
Copy link
Contributor

I'm -1 on this, if NaN == NaN is false, then deepEqual([NaN], [NaN]) should also be false.

@evanlucas
Copy link
Contributor

I agree with @brendanashworth . If you need to verify an array, then you could loop and assert(isNaN(value)) for each item

@thefourtheye thefourtheye deleted the assert-nan branch August 21, 2015 05:36
@thefourtheye
Copy link
Contributor Author

@evanlucas What if only few of the items are NaN?

@thefourtheye
Copy link
Contributor Author

I am still not convinced, but closing as this is -1ed.

@cjihrig
Copy link
Contributor

cjihrig commented Aug 21, 2015

@thefourtheye I'd encourage you to release this on npm.

@thefourtheye
Copy link
Contributor Author

@cjihrig Even chai had the same problem. I am trying to fix it. chaijs/chai#508

@evanlucas
Copy link
Contributor

@thefourtheye then you end up doing something like iterating over all items and checking isNaN on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert Issues and PRs related to the assert subsystem. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants