Skip to content

Commit caa77fb

Browse files
rubennortefacebook-github-bot
authored andcommitted
Prevent console.table from modifying passed values (facebook#48590)
Summary: Pull Request resolved: facebook#48590 Changelog: [General][Fixed] Modified `console.table` to avoid mutating the received argument. Reviewed By: sammy-SC Differential Revision: D67791795 fbshipit-source-id: a889fe95914cf7850e6429742845b126917babc7
1 parent 7f985f2 commit caa77fb

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/polyfills/__tests__/console-itest.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,40 @@ fourth `,
159159
global.nativeLoggingHook = originalNativeLoggingHook;
160160
}
161161
});
162+
163+
it('should not modify the logged value', () => {
164+
const originalNativeLoggingHook = global.nativeLoggingHook;
165+
global.nativeLoggingHook = jest.fn();
166+
167+
// TODO: replace with `beforeEach` when supported.
168+
try {
169+
const array = [
170+
{name: 'First', value: 500},
171+
{name: 'Second', value: 600},
172+
{name: 'Third', value: 700},
173+
{name: 'Fourth', value: 800, extraValue: true},
174+
];
175+
const originalArrayValue = JSON.parse(JSON.stringify(array));
176+
177+
console.table(array);
178+
179+
expect(array).toEqual(originalArrayValue);
180+
181+
const object = {
182+
first: {name: 'First', value: 500},
183+
second: {name: 'Second', value: 600},
184+
third: {name: 'Third', value: 700},
185+
fourth: {name: 'Fourth', value: 800, extraValue: true},
186+
};
187+
188+
const originalObjectValue = JSON.parse(JSON.stringify(object));
189+
190+
console.table(object);
191+
192+
expect(object).toEqual(originalObjectValue);
193+
} finally {
194+
global.nativeLoggingHook = originalNativeLoggingHook;
195+
}
196+
});
162197
});
163198
});

packages/polyfills/console.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ function consoleTablePolyfill(rows) {
440440
rows = [];
441441
for (var key in data) {
442442
if (data.hasOwnProperty(key)) {
443-
var row = data[key];
443+
var row = Object.assign({}, data[key]);
444444
row[OBJECT_COLUMN_NAME] = key;
445445
rows.push(row);
446446
}

0 commit comments

Comments
 (0)