refactor(devtools): prevent exeception on state serializer (#55061)

`Object.getPrototypeOf(obj)` returns `null` if `obj` is an empty object. `Object.getOwnPropertyDescriptors` throws on `null`/`undefined`

PR Close #55061
This commit is contained in:
Matthieu Riegler
2024-03-26 22:41:18 -07:00
committed by Dylan Hunn
parent e02bcf89cf
commit 9a9ce0d419
2 changed files with 9 additions and 1 deletions
@@ -21,7 +21,8 @@ export function getKeys(obj: {}): string[] {
obj = unwrapSignal(obj);
const properties = Object.getOwnPropertyNames(obj);
const prototypeMembers = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(obj));
// Object.getPrototypeOf can return null, on empty objectwithout prototype for example
const prototypeMembers = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(obj) ?? {});
const ignoreList = ['__proto__'];
const gettersAndSetters = Object.keys(prototypeMembers).filter((methodName) => {
@@ -531,6 +531,13 @@ describe('deeplySerializeSelectedProperties', () => {
expect(getKeys(instance)).toEqual(['baz', 'foo', 'bar']);
});
it('getKeys should not throw on empty object without prototype', () => {
// creates an object without a prototype
const instance = Object.create(null);
expect(getKeys(instance)).toEqual([]);
});
it('getKeys would ignore getters and setters for "__proto__"', () => {
const instance = {
baz: 2,