chaijs / deep-eql

Improved deep equality testing for Node.js and the browser.
MIT License
108 stars 39 forks source link

Comparing Properties with getters only #52

Open ihachani opened 6 years ago

ihachani commented 6 years ago

I have an immutable data structure defined as follow:

let DataStructure = function (name, description, imageUrl, linkUrl) {
    let _name = name;
    let _description = description;
    let _imageUrl = imageUrl;
    let _linkUrl = linkUrl;

    Object.defineProperty(this, 'name', {
        get: () => _name,
    });

    Object.defineProperty(this, 'description', {
        get: () => _description,
    });

    Object.defineProperty(this, 'imageUrl', {
        get: () => _imageUrl,
    });

    Object.defineProperty(this, 'linkUrl', {
        get: () => _linkUrl,
    });
};

Comparing to object instances with different values always returns true when it should return false.

keithamus commented 6 years ago

Hey @ihachani thanks for the issue.

This is because those properties are non-enumerable, and deep-eql ignores non-enumerable properties. This is something we could perhaps change, but for now you can simple add enumerable: true to your defineProperty calls. Alternatively you could write a helper to handle these objects.

ihachani commented 6 years ago

I added enumerable: true and it is working now. May I ask why deep-eql ignores enumerables? What would I gain/lose when I set it to true. Thanks.

keithamus commented 6 years ago

It was a design decision, somewhat arbitrary. It may change in the future. What you gain/lose can probably best be explained by MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable