Skip to content

Commit ec4d592

Browse files
authored
fix #2589: avoid invoking getter as side-effect (#2592)
of creating restorerer function. Check for the odd case where we actually force setting a value through accessor functions and only look up the value at that time. The alternative would be more elaborate, check if the descriptor had a `get` or a `value` field, and then decide, but this should do the trick.
1 parent 9972e1e commit ec4d592

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/sinon/sandbox.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ function Sandbox(opts = {}) {
243243
*/
244244
function getFakeRestorer(object, property, forceAssignment = false) {
245245
const descriptor = getPropertyDescriptor(object, property);
246-
const value = object[property];
246+
const value = forceAssignment && object[property];
247247

248248
function restorer() {
249249
if (forceAssignment) {

test/sandbox-test.js

+14
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,20 @@ describe("Sandbox", function () {
12961296
},
12971297
);
12981298
});
1299+
1300+
it("do not call getter whilst replacing getter", function () {
1301+
const sandbox = this.sandbox;
1302+
const fake = sandbox.fake.returns("bar");
1303+
const object = {
1304+
get foo() {
1305+
return fake();
1306+
},
1307+
};
1308+
1309+
sandbox.replaceGetter(object, "foo", () => "replacement");
1310+
1311+
refute(fake.called);
1312+
});
12991313
});
13001314

13011315
describe(".replaceSetter", function () {

0 commit comments

Comments
 (0)