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 issue 2534: spies are not restored #2535

Merged
merged 3 commits into from
Aug 11, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 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 @@ -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
28 changes: 26 additions & 2 deletions lib/sinon/util/core/get-property-descriptor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
"use strict";

module.exports = function getPropertyDescriptor(object, property) {
/* eslint-disable jsdoc/valid-types */
/*
* The following type def is strictly an illegal JSDoc, but the expression forms a
* legal Typescript union type and is understood by Visual Studio and the IntelliJ
* family of editors. The "TS" flavor of JSDoc is becoming the de-facto standard these
* days for that reason (and the fact that JSDoc is essentially unmaintained)
*/

/**
* @typedef {{isOwn: boolean} & PropertyDescriptor} SinonPropertyDescriptor
* a slightly enriched property descriptor
* @property {boolean} isOwn true if the descriptor is owned by this object, false if it comes from the prototype
*/
/* eslint-enable jsdoc/valid-types */

/**
* Returns a slightly modified property descriptor that one can tell is from the object or the prototype
*
* @param {*} object
* @param {string} property
* @returns {SinonPropertyDescriptor}
*/
function getPropertyDescriptor(object, property) {
let proto = object;
let descriptor;
const isOwn = Boolean(
Expand All @@ -19,4 +41,6 @@ module.exports = function getPropertyDescriptor(object, property) {
}

return descriptor;
};
}

module.exports = getPropertyDescriptor;
15 changes: 15 additions & 0 deletions test/issues/issues-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,4 +899,19 @@ 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
});
});
33 changes: 32 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 @@ -2221,6 +2221,37 @@ describe("Sandbox", function () {

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);
});
});

describe(".assert", function () {
Expand Down
Loading