Closed
Description
The 'extend' function from 'util/core' is not handling property descriptors using accessors properly.
The issue was introduced by the following commit: f981192
The below test can be used to duplicate the issue. The object 'obj' has a 'pub' property that is defined using a getter and a setter. Once 'extend' is called, the property descriptor is recreated using 'value' and ignoring the accessors.
import extend from 'sinon/lib/sinon/util/core/extend';
describe('extend', () => {
it('must preserve accessors', () => {
const obj = {
_priv: 1
};
Object.defineProperty(obj, 'pub', {
configurable: true,
get: () => obj._priv,
set: value => {
obj._priv = value;
}
});
const newObj = {};
extend(newObj, obj);
expect(newObj['pub']).toBe(1);
});
});
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
fatso83 commentedon Jul 9, 2021
Good find!
Copy over acessor properties to target object sinonjs#2387
sauravazad commentedon Jul 17, 2021
There is an inherent mistake in the implementation as it does not distinguishes between Data property and Accessor property
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#properties
Associates a key with a value, and has the following attributes:
Attributes of a data property
Associates a key with one of two accessor functions (get and set) to retrieve or store a value, and has the following attributes:
Attributes of an accessor property
I have raised a PR to fix the behavior #2391.
Copy over acessor properties to target object sinonjs#2387
Copy over acessor properties to target object sinonjs#2387
Copy over acessor properties to target object #2387
mroderick commentedon Jul 27, 2021
This has been fixed by #2391 and published to the npm registry as
sinon@11.1.2