Skip to content

Commit

Permalink
Fix issue #2534
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Aug 11, 2023
1 parent b78bf38 commit 02a6cd4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/sinon/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ function Sandbox() {
delete object[property];
}
}

restorer.object = object;
restorer.property = property;
return restorer;
Expand Down Expand Up @@ -263,7 +264,7 @@ function Sandbox() {
throw new TypeError(
`Cannot replace ${typeof object[
property
]} with ${typeof replacement}`
]} with ${typeof replacement}`
);
}

Expand Down Expand Up @@ -355,8 +356,7 @@ function Sandbox() {
};

function commonPostInitSetup(args, spy) {
const object = args[0];
const property = args[1];
const [object, property, types] = args;

const isSpyingOnEntireObject =
typeof property === "undefined" && typeof object === "object";
Expand All @@ -369,6 +369,11 @@ function Sandbox() {
});

usePromiseLibrary(promiseLib, ownMethods);
} else if (Array.isArray(types)) {
for(const accessorType of types) {
addToCollection(spy[accessorType])
usePromiseLibrary(promiseLib, spy[accessorType]);
}
} else {
addToCollection(spy);
usePromiseLibrary(promiseLib, spy);
Expand Down
13 changes: 13 additions & 0 deletions test/issues/issues-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,4 +899,17 @@ describe("issues", function () {
this.sandbox.restore();
});
});

it("#2534 - spies on accessors are not being cleaned up", function() {
const object = {
get prop(){ return "bar"},
};
const spy = sinon.spy(object, "prop", ["get"]);
/* eslint-disable no-unused-expressions */
object.prop;
assert.equals(spy.get.callCount,1);
sinon.restore();
object.prop;
assert.equals(spy.get.callCount,1); // should remain unchanged
})
});
30 changes: 29 additions & 1 deletion test/sandbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2205,7 +2205,7 @@ describe("Sandbox", function () {
assert.equals(object.prop, "blabla");
});

it("allows restoring setters", function () {
it("allows putting setters on fields and subsequently restoring them", function () {
const object = {
prop: "bar",
};
Expand All @@ -2220,6 +2220,34 @@ describe("Sandbox", function () {
object.prop = "bla";

assert.equals(object.prop, "bla");
})

it("allows replacing setters on fields and subsequently restoring them", function () {
const object = {
get prop(){ return "bar"},
};

const sandbox = new Sandbox();
const getter = sandbox.spy(() => "foobar");
sandbox.stub(object, "prop").get(getter);
assert.equals(object.prop, "foobar");
assert.equals(getter.callCount,1);


sandbox.restore();
assert.equals(object.prop, "bar");
});

it("allows spying on accessors and subsequently restoring them", function () {
const object = {
get prop(){ return "bar"},
};
const sandbox = new Sandbox();
const spy = sandbox.spy(object, "prop", ["get"]);
sandbox.restore();
const descriptor = Object.getOwnPropertyDescriptor(object, "prop");
const getter = descriptor.get
refute.equals(getter,spy.get);
});
});

Expand Down

0 comments on commit 02a6cd4

Please sign in to comment.