toEqual behave differently with DOM elements in PhantomJS, Chrome and Firefox #606

Closed
alexeibs opened this Issue Jun 6, 2014 · 6 comments

Comments

Projects
None yet
3 participants
@alexeibs
Contributor

alexeibs commented Jun 6, 2014

There is a simple spec to reproduce the bug.

describe("Compare of DOM elements", function() {
    var d1 = document.createElement("div");
    d1.appendChild(document.createTextNode(1));
    var d2 = document.createElement("div");
    d2.appendChild(document.createTextNode(2));

    var d3 = document.createElement("div");
    var d4 = document.createElement("div");

    it("should pass in Chrome and PhantomJS, fail in Firefox", function() {
        expect(d1).not.toEqual(d2);
        expect([d1, d2]).not.toEqual([d2, d1]);
        expect([d1, d2]).toEqual([d1, d2]);
    });
    it("should pass in PhantomJS, fail in Chrome and Firefox", function() {
        expect(d3).not.toEqual(d4);
        expect([d3, d4]).not.toEqual([d4, d3]);
        expect([d3, d4]).toEqual([d3, d4]);
    });
});

I've tested this in PhantomJS 1.9, Firefox 29, Chrome 35 under Windows 7 x64. I use Jasmine from grunt-contrib-jasmine 0.6.5

@infews

This comment has been minimized.

Show comment Hide comment
@infews

infews Jun 23, 2014

Contributor

I can't say I'm surprised. Equality of DOM nodes cross browser is a very hard problem to stay on top of.

I'm not trying to provide a cop-out type of answer, but have you tried testing at a different level of granularity? That might result in less brittle tests.

Contributor

infews commented Jun 23, 2014

I can't say I'm surprised. Equality of DOM nodes cross browser is a very hard problem to stay on top of.

I'm not trying to provide a cop-out type of answer, but have you tried testing at a different level of granularity? That might result in less brittle tests.

@alexeibs

This comment has been minimized.

Show comment Hide comment
@alexeibs

alexeibs Jun 23, 2014

Contributor

Actually I dont need to find equality of DOM nodes. toEqual is convenient to compare arrays. I've written a simple helper that covers my needs:

function expectObjects(a, b) {
    expect(a.length).toEqual(b.length);
    for (var i = 0, imax = a.length; i < imax; ++i)
        expect(a[i]).toBe(b[i]);
}
Contributor

alexeibs commented Jun 23, 2014

Actually I dont need to find equality of DOM nodes. toEqual is convenient to compare arrays. I've written a simple helper that covers my needs:

function expectObjects(a, b) {
    expect(a.length).toEqual(b.length);
    for (var i = 0, imax = a.length; i < imax; ++i)
        expect(a[i]).toBe(b[i]);
}
@slackersoft

This comment has been minimized.

Show comment Hide comment
@slackersoft

slackersoft Aug 18, 2014

Owner

I just ran your example in chrome, firefox, and phantom and got passes in all three browsers with jasmine 2.0.1. It seems like maybe this is fixed now?

Owner

slackersoft commented Aug 18, 2014

I just ran your example in chrome, firefox, and phantom and got passes in all three browsers with jasmine 2.0.1. It seems like maybe this is fixed now?

@alexeibs

This comment has been minimized.

Show comment Hide comment
@alexeibs

alexeibs Aug 19, 2014

Contributor

I've run my example again with jasmine 2.0.1 but I still see the same behaviour as in my first comment. Browsers: Chrome 36, Firefox 31, PhantomJS 1.9.7, all under Windows 7 x64. My package.json:

{
  "name": "jsProjectStub",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "0.4.5",
    "grunt-contrib-jshint": "0.10.0",
    "grunt-contrib-csslint": "0.2.0",
    "grunt-contrib-jasmine": "0.7.0",
    "jshint-path-reporter": "0.1.3"
  }
}
Contributor

alexeibs commented Aug 19, 2014

I've run my example again with jasmine 2.0.1 but I still see the same behaviour as in my first comment. Browsers: Chrome 36, Firefox 31, PhantomJS 1.9.7, all under Windows 7 x64. My package.json:

{
  "name": "jsProjectStub",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "0.4.5",
    "grunt-contrib-jshint": "0.10.0",
    "grunt-contrib-csslint": "0.2.0",
    "grunt-contrib-jasmine": "0.7.0",
    "jshint-path-reporter": "0.1.3"
  }
}
@slackersoft

This comment has been minimized.

Show comment Hide comment
@slackersoft

slackersoft Aug 22, 2014

Owner

You're right, it still breaks. I just noticed I had jasmine-jquery in my test project, and that adds a custom equality tester to match dom nodes better by using jquerys is function.

I'd be happy to look at a pull request that implemented something like that but didn't rely on jquery.

Owner

slackersoft commented Aug 22, 2014

You're right, it still breaks. I just noticed I had jasmine-jquery in my test project, and that adds a custom equality tester to match dom nodes better by using jquerys is function.

I'd be happy to look at a pull request that implemented something like that but didn't rely on jquery.

@slackersoft

This comment has been minimized.

Show comment Hide comment
@slackersoft

slackersoft Dec 16, 2014

Owner

#657 has been merged, closing this.

Owner

slackersoft commented Dec 16, 2014

#657 has been merged, closing this.

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