From dfa94c70c1934205e278b5966159b23aa89a7814 Mon Sep 17 00:00:00 2001 From: suke Date: Tue, 26 Apr 2022 23:31:44 +0900 Subject: [PATCH] toEqual does not compare symbols that cannot be enumerated --- lib/jasmine-core/jasmine.js | 11 +++++++++-- spec/core/matchers/toEqualSpec.js | 23 +++++++++++++++++++++++ src/core/matchers/matchersUtil.js | 11 +++++++++-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 1d0be920..25dd94f4 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -5589,8 +5589,15 @@ getJasmineRequireObj().MatchersUtil = function(j$) { keys.push(key); } } - // eslint-disable-next-line compat/compat - return keys.concat(Object.getOwnPropertySymbols(o)); + + var symbols = Object.getOwnPropertySymbols(o); + for (var i = 0; i < symbols.length; i++) { + if (o.propertyIsEnumerable(symbols[i])) { + keys.push(symbols[i]); + } + } + + return keys; })(obj); if (!isArray) { diff --git a/spec/core/matchers/toEqualSpec.js b/spec/core/matchers/toEqualSpec.js index a6ca1ae7..3ecd5fbe 100644 --- a/spec/core/matchers/toEqualSpec.js +++ b/spec/core/matchers/toEqualSpec.js @@ -1042,6 +1042,29 @@ describe('toEqual', function() { // == Symbols == describe('Symbols', function() { + it('Enumerable symbols are compared', function() { + const sym = Symbol('foo'); + const actual = {}; + Object.defineProperty(actual, sym, { + value: '', + enumerable: true + }); + const expected = { [sym]: '' }; + + expect(actual).toEqual(expected); + }); + + it('Symbols that cannot be enumerated are not compared ', function() { + const sym = Symbol('foo'); + const actual = {}; + Object.defineProperty(actual, sym, { + value: '', + enumerable: false + }); + const expected = {}; + expect(actual).toEqual(expected); + }); + it('Fails if Symbol compared to Object', function() { const sym = Symbol('foo'); const obj = {}; diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index c6f297f2..7a48e52f 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -532,8 +532,15 @@ getJasmineRequireObj().MatchersUtil = function(j$) { keys.push(key); } } - // eslint-disable-next-line compat/compat - return keys.concat(Object.getOwnPropertySymbols(o)); + + var symbols = Object.getOwnPropertySymbols(o); + for (var i = 0; i < symbols.length; i++) { + if (o.propertyIsEnumerable(symbols[i])) { + keys.push(symbols[i]); + } + } + + return keys; })(obj); if (!isArray) {