Skip to content

Commit

Permalink
Prevent console.table from modifying passed values
Browse files Browse the repository at this point in the history
Summary: Changelog: [General][Fixed] Modified `console.table` to avoid mutating the received argument.

Differential Revision: D67791795
  • Loading branch information
rubennorte authored and facebook-github-bot committed Jan 10, 2025
1 parent 31c5647 commit c0cb09f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/polyfills/__tests__/console-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,40 @@ fourth `,
global.nativeLoggingHook = originalNativeLoggingHook;
}
});

it('should not modify the logged value', () => {
const originalNativeLoggingHook = global.nativeLoggingHook;
global.nativeLoggingHook = jest.fn();

// TODO: replace with `beforeEach` when supported.
try {
const array = [
{name: 'First', value: 500},
{name: 'Second', value: 600},
{name: 'Third', value: 700},
{name: 'Fourth', value: 800, extraValue: true},
];
const originalArrayValue = JSON.parse(JSON.stringify(array));

console.table(array);

expect(array).toEqual(originalArrayValue);

const object = {
first: {name: 'First', value: 500},
second: {name: 'Second', value: 600},
third: {name: 'Third', value: 700},
fourth: {name: 'Fourth', value: 800, extraValue: true},
};

const originalObjectValue = JSON.parse(JSON.stringify(object));

console.table(object);

expect(object).toEqual(originalObjectValue);
} finally {
global.nativeLoggingHook = originalNativeLoggingHook;
}
});
});
});
2 changes: 1 addition & 1 deletion packages/polyfills/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ function consoleTablePolyfill(rows) {
rows = [];
for (var key in data) {
if (data.hasOwnProperty(key)) {
var row = data[key];
var row = Object.assign({}, data[key]);
row[OBJECT_COLUMN_NAME] = key;
rows.push(row);
}
Expand Down

0 comments on commit c0cb09f

Please sign in to comment.