Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): Allow to override proxy properties with defineProperty #16598

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,29 @@ test('allows to override a property from a layer', () => {
expect(proxy.prop).toBe('override')
})

test('allows to override a property from a layer using defineProperty', () => {
const target = {} as Record<string, unknown>

const proxy = createCompositeProxy(target, [
{
getKeys() {
return ['prop']
},

getPropertyValue() {
return 'from proxy'
},
},
])

Object.defineProperty(proxy, 'prop', {
value: 'override',
})

expect(target.prop).toBe('override')
expect(proxy.prop).toBe('override')
})

test('does not allow to overriding property from a layer if it is non writable', () => {
const target = {} as Record<string, unknown>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ export function createCompositeProxy<T extends object>(target: T, layers: Compos
}
return defaultPropertyDescriptor
},

defineProperty(target, property, attributes) {
overwrittenKeys.add(property)
return Reflect.defineProperty(target, property, attributes)
},
})

proxy[customInspect] = function (depth: number, options: InspectOptions, defaultInspect: typeof inspect = inspect) {
Expand Down