From bade6b0ecccb59c8a3f53c4666f4dd8f8c861f71 Mon Sep 17 00:00:00 2001 From: Cristian Penarrieta Date: Mon, 5 Jun 2017 01:25:08 -0700 Subject: [PATCH] compare objects with Symbol keys (#3437) * compare objects with Symbol keys * Update symbol-in-objects-test.js --- .../src/__tests__/symbol-in-objects-test.js | 44 +++++++++++++++++++ packages/jest-matchers/src/jasmine-utils.js | 2 +- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 packages/jest-matchers/src/__tests__/symbol-in-objects-test.js diff --git a/packages/jest-matchers/src/__tests__/symbol-in-objects-test.js b/packages/jest-matchers/src/__tests__/symbol-in-objects-test.js new file mode 100644 index 000000000000..3b247f991346 --- /dev/null +++ b/packages/jest-matchers/src/__tests__/symbol-in-objects-test.js @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @emails oncall+jsinfra + */ + +'use strict'; + +describe('Symbol in objects', () => { + test('should compare objects with Symbol keys', () => { + const sym = Symbol('foo'); + const obj1 = {[sym]: 'one'}; + const obj2 = {[sym]: 'two'}; + const obj3 = {[sym]: 'one'}; + + expect(obj1).toEqual(obj3); + expect(obj1).not.toEqual(obj2); + }); + + test('should compare objects with mixed keys and Symbol', () => { + const sym = Symbol('foo2'); + const obj1 = {foo: 2, [sym]: 'one'}; + const obj2 = {foo: 2, [sym]: 'two'}; + const obj3 = {foo: 2, [sym]: 'one'}; + + expect(obj1).toEqual(obj3); + expect(obj1).not.toEqual(obj2); + }); + + test('should compare objects with different Symbol keys', () => { + const sym = Symbol('foo'); + const sym2 = Symbol('foo'); + const obj1 = {[sym]: 'one'}; + const obj2 = {[sym2]: 'one'}; + const obj3 = {[sym]: 'one'}; + + expect(obj1).toEqual(obj3); + expect(obj1).not.toEqual(obj2); + }); +}); diff --git a/packages/jest-matchers/src/jasmine-utils.js b/packages/jest-matchers/src/jasmine-utils.js index 7cc6db6e6ab5..3cda741d4802 100644 --- a/packages/jest-matchers/src/jasmine-utils.js +++ b/packages/jest-matchers/src/jasmine-utils.js @@ -215,7 +215,7 @@ function keys(obj, isArray) { keys.push(key); } } - return keys; + return keys.concat(Object.getOwnPropertySymbols(o)); })(obj); if (!isArray) {