From 533258225dea523bb4256bd4c256e8c1f322bfaa Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 2 Dec 2018 23:54:34 -0500 Subject: [PATCH] pretty-format: Omit non-enumerable symbol properties (#7448) * pretty-format: Omit non-enumerable symbol properties * Update CHANGELOG.md * Correct CHANGELOG.md * Move change line to other breaking under fixes --- CHANGELOG.md | 1 + .../src/__tests__/prettyFormat.test.js | 24 +++++++++++++++++++ packages/pretty-format/src/collections.js | 5 +++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 622c61c3e303..4093c63ceafb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ - `[jest-cli]` [**BREAKING**] Do not use `text-summary` coverage reporter by default if other reporters are configured ([#7058](https://github.com/facebook/jest/pull/7058)) - `[jest-mock]` [**BREAKING**] Fix bugs with mock/spy result tracking of recursive functions ([#6381](https://github.com/facebook/jest/pull/6381)) - `[jest-haste-map]` [**BREAKING**] Recover files correctly after haste name collisions are fixed ([#7329](https://github.com/facebook/jest/pull/7329)) +- `[pretty-format]` [**BREAKING**] Omit non-enumerable symbol properties ([#7448](https://github.com/facebook/jest/pull/7448)) - `[expect]` Standardize file naming in `expect` ([#7306](https://github.com/facebook/jest/pull/7306)) - `[jest-each]` Add empty array validation check ([#7249](https://github.com/facebook/jest/pull/7249)) - `[jest-cli]` Interrupt tests if interactive watch plugin key is pressed ([#7222](https://github.com/facebook/jest/pull/7222)) diff --git a/packages/pretty-format/src/__tests__/prettyFormat.test.js b/packages/pretty-format/src/__tests__/prettyFormat.test.js index 9d440352dbe5..12dafcdf35ac 100644 --- a/packages/pretty-format/src/__tests__/prettyFormat.test.js +++ b/packages/pretty-format/src/__tests__/prettyFormat.test.js @@ -261,6 +261,30 @@ describe('prettyFormat()', () => { ); }); + it('prints an object without non-enumerable properties which have string key', () => { + const val: any = { + enumerable: true, + }; + const key = 'non-enumerable'; + Object.defineProperty(val, key, { + enumerable: false, + value: false, + }); + expect(prettyFormat(val)).toEqual('Object {\n "enumerable": true,\n}'); + }); + + it('prints an object without non-enumerable properties which have symbol key', () => { + const val: any = { + enumerable: true, + }; + const key = Symbol('non-enumerable'); + Object.defineProperty(val, key, { + enumerable: false, + value: false, + }); + expect(prettyFormat(val)).toEqual('Object {\n "enumerable": true,\n}'); + }); + it('prints an object with sorted properties', () => { /* eslint-disable sort-keys */ const val = {b: 1, a: 2}; diff --git a/packages/pretty-format/src/collections.js b/packages/pretty-format/src/collections.js index 7a0f22561a84..fe802b59594e 100644 --- a/packages/pretty-format/src/collections.js +++ b/packages/pretty-format/src/collections.js @@ -162,7 +162,10 @@ export function printObjectProperties( ): string { let result = ''; let keys = Object.keys(val).sort(); - const symbols = getSymbols(val); + const symbols = getSymbols(val).filter( + //$FlowFixMe because property enumerable is missing in undefined + symbol => Object.getOwnPropertyDescriptor(val, symbol).enumerable, + ); if (symbols.length) { keys = keys.filter(key => !isSymbol(key)).concat(symbols);