From 9d80377fe3aa04fadbc939ac2b2156c19a08c9b2 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Sat, 7 May 2022 10:42:29 -0700 Subject: [PATCH] Fixed exception when comparing arrays with Symbol keys * Fixes #1966 --- lib/jasmine-core/jasmine.js | 2 +- spec/core/matchers/matchersUtilSpec.js | 22 ++++++++++++++++++++++ src/core/matchers/matchersUtil.js | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index e132b963..84431f6d 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -5603,7 +5603,7 @@ getJasmineRequireObj().MatchersUtil = function(j$) { var extraKeys = []; for (var i = 0; i < allKeys.length; i++) { - if (!/^[0-9]+$/.test(allKeys[i])) { + if (typeof allKeys[i] === 'symbol' || !/^[0-9]+$/.test(allKeys[i])) { extraKeys.push(allKeys[i]); } } diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index b730c9c5..2229aafc 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -125,6 +125,28 @@ describe('matchersUtil', function() { expect(matchersUtil.equals(one, two)).toBe(true); }); + it('handles symbol keys in Arrays', function() { + const matchersUtil = new jasmineUnderTest.MatchersUtil(), + sym = Symbol('foo'), + arr1 = []; + let arr2 = []; + + arr1[sym] = 'bar'; + arr2[sym] = 'bar'; + expect(matchersUtil.equals(arr1, arr2)).toBe(true); + + arr2[sym] = 'baz'; + expect(matchersUtil.equals(arr1, arr2)).toBe(false); + + arr2 = []; + arr2[Symbol('foo')] = 'bar'; + expect(matchersUtil.equals(arr1, arr2)).toBe(false); + + arr2 = []; + arr2['foo'] = 'bar'; + expect(matchersUtil.equals(arr1, arr2)).toBe(false); + }); + it('passes for Errors that are the same type and have the same message', function() { const matchersUtil = new jasmineUnderTest.MatchersUtil(); expect(matchersUtil.equals(new Error('foo'), new Error('foo'))).toBe( diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index 774c7d9b..e3083322 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -546,7 +546,7 @@ getJasmineRequireObj().MatchersUtil = function(j$) { var extraKeys = []; for (var i = 0; i < allKeys.length; i++) { - if (!/^[0-9]+$/.test(allKeys[i])) { + if (typeof allKeys[i] === 'symbol' || !/^[0-9]+$/.test(allKeys[i])) { extraKeys.push(allKeys[i]); } }