Skip to content

embind enums aren't compatible with jasmine equality checks #19387

@mitchellwills

Description

@mitchellwills

Version of emscripten/emsdk: 3.1.35

All enum values of a given enum type generated by embind are considered equivalent by jasmine. Jasmine does object equality by doing a deep equals on most objects; however, this relies on being able to iterate over the object keys (using Object.keys). embind enum values are all objects with a single non-enumerable property with the underlying value. Since the value property isn't enumerable the object looks the same. For single values this is somewhat ok because the toBe matcher can be used instead; however, it becomes problematic when comparing the quality of objects/arrays using toEquals because the same logic is applied recursively.

For example

module.MyEnum.VALUE1 === module.MyEnum.VALUE2 // false
module.MyEnum.VALUE1 == module.MyEnum.VALUE2 // false
expect(module.MyEnum.VALUE1).toBe(module.MyEnum.VALUE2);  // assertion failes
expect(module.MyEnum.VALUE1).toEqual(module.MyEnum.VALUE2);  // assertion incorrectly passes
expect([module.MyEnum.VALUE1]).toEqual([module.MyEnum.VALUE2]);  // assertion incorrectly passes

The setup code simplifies to roughly something like the following:

  it('enum equality', () => {
    const MyEnum = {};

    var Value1 = Object.create(MyEnum.constructor.prototype, {
      value: {value: 1},  // not enumerable
    });
    var Value2 = Object.create(MyEnum.constructor.prototype, {
      value: {value: 2},  // not enumerable
    });

    expect(Value1 !== Value2).toBe(true);
    expect(Value1 != Value2).toBe(true);
    expect(Value1).toEqual(Value2);  // always passes
    expect([Value1]).toEqual([Value2]);  // always passes
  });

One option to fix this could be to simply make the value property enumerable, but not sure if that would be a breaking change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions