-0 should equal 0 #579

Closed
jonatr35 opened this Issue Apr 21, 2014 · 5 comments

Projects

None yet

4 participants

@jonatr35

I've written a unit test for a function were I expect my returned value to be rounded to 0 (example -0.4 should return 0). I've written my unit test as expect(result).toEqual(0) and it is failing.

When attempting to debug I eventually found out that it was failing because it thought that -0 did not equal 0. It did however pass when using expect(result).toBe(0), however I should still be able to use expect(-0).toEqual(0).

@sheelc
Contributor
sheelc commented Apr 24, 2014

There's more discussion about this issue here which can hopefully give some context as to why -0 does not toEqual 0. There's still some ongoing discussion if this is the correct decision.

A quick summary though, toBe is JavaScript's === while toEqual is Underscore.js's notion of what equality means. The error message is better on master now where it does print out that one of the values is '-0' which can help users debug this more quickly.

@infews
Contributor
infews commented Jun 23, 2014

@jonatr35 - is this issue closeable? We think we've fixed this on master.

@jonatr35

I just cloned the master and created a spec to test that -0 should equal 0 but it is still failing. I am seeing the new message that says -0 should equal 0 however.

When testing I am using the jasmine files in lib\jasmine-core. Just want to make sure that is correct. Below is my spec file.

describe("Verify that -0 equals 0", function() {

    describe("-0 should EQUAL 0", function() {
        it("should work hard coded", function() {
            expect(-0).toEqual(0);
        })

        it("should work with Math.ceil", function() {
            expect(Math.ceil(-0.001)).toEqual(0);
        })
    })

    describe("-0 should BE 0", function() {
        it("should work hard coded", function() {
            expect(-0).toBe(0);
        })

        it("should work with Math.ceil", function() {
            expect(Math.ceil(-0.001)).toBe(0);
        })
    })
});
@sheelc
Contributor
sheelc commented Jun 25, 2014

Yes, you are demonstrating the current desired behavior on master. We now print '-0' specifically to indicate to the user why the spec is failing (whereas in the past it was simply showing '0'), but based on discussion in the other issue (#496) we're leaning towards mimicking the Underscore.js behavior, which means expect(0).toEqual(-0) is expected to fail.

There is still some ongoing discussion in #496 if this is the correct behavior, however.

@slackersoft
Member

We haven't heard any further feedback on this or #496 so I'm going to close this with displaying -0 as the desired behavior as @sheelc mentioned. If we hear more feedback in either issue we can reconsider.

@slackersoft slackersoft closed this Nov 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment