Skip to content

Commit

Permalink
fix(utils): fix object diff with getter only property (#5466)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Apr 2, 2024
1 parent 91b06cc commit 366d97c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/utils/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ export function clone<T>(
if (!descriptor)
continue
const cloned = clone((val as any)[k], seen, options)
if ('get' in descriptor) {
if (options.forceWritable) {
Object.defineProperty(out, k, {
enumerable: descriptor.enumerable,
configurable: true,
writable: true,
value: cloned,
})
}
else if ('get' in descriptor) {
Object.defineProperty(out, k, {
...descriptor,
get() {
Expand All @@ -122,7 +130,6 @@ export function clone<T>(
else {
Object.defineProperty(out, k, {
...descriptor,
writable: options.forceWritable ? true : descriptor.writable,
value: cloned,
})
}
Expand Down
28 changes: 28 additions & 0 deletions test/core/test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,34 @@ test('asymmetric matcher in nested', () => {
`)
})

test('getter only property', () => {
setupColors(getDefaultColors())
const x = { normalProp: 1 }
const y = { normalProp: 2 }
Object.defineProperty(x, 'getOnlyProp', {
enumerable: true,
get: () => ({ a: 'b' }),
})
Object.defineProperty(y, 'getOnlyProp', {
enumerable: true,
get: () => ({ a: 'b' }),
})
expect(
getErrorDiff(x, y),
).toMatchInlineSnapshot(`
"- Expected
+ Received
Object {
"getOnlyProp": Object {
"a": "b",
},
- "normalProp": 2,
+ "normalProp": 1,
}"
`)
})

function getErrorDiff(actual: unknown, expected: unknown) {
try {
expect(actual).toEqual(expected)
Expand Down

0 comments on commit 366d97c

Please sign in to comment.