diff --git a/docs/release-source/release/sandbox.md b/docs/release-source/release/sandbox.md index c695ecd65..2ba60be11 100644 --- a/docs/release-source/release/sandbox.md +++ b/docs/release-source/release/sandbox.md @@ -241,6 +241,7 @@ console.log(myObject.myMethod()); Usually one intends to _replace_ the value or getter of a field, but there are use cases where one actually wants to _assign_ a value to a property using an existing setter. `#replace.usingAccessor(object, property, value)` will do just that; pass the value into setter function and vice-versa use the getter to get the value used for restoring later on. ##### Use case: no-frills dependency injection in ESM with cleanup + One use case can be to conveniently allow ESM module stubbing using pure dependency injection, having Sinon help you with the cleanup, without resorting to external machinery such as module loaders or require hooks (see [#2403](https://github.com/sinonjs/sinon/issues/2403)). This would then work regardless of bundler, browser or server environment. #### `sandbox.replaceGetter(object, property, replacementFunction);` diff --git a/lib/sinon/sandbox.js b/lib/sinon/sandbox.js index 6fd948f47..e5e2c190f 100644 --- a/lib/sinon/sandbox.js +++ b/lib/sinon/sandbox.js @@ -50,7 +50,7 @@ function verifySameType(object, property, replacement) { throw new TypeError( `Cannot replace ${typeof object[ property - ]} with ${typeof replacement}` + ]} with ${typeof replacement}` ); } } @@ -280,17 +280,18 @@ function Sandbox() { verifyNotReplaced(object, property); // store a function for restoring the replaced property - push( - fakeRestorers, - getFakeRestorer(object, property) - ); + push(fakeRestorers, getFakeRestorer(object, property)); object[property] = replacement; return replacement; }; - sandbox.replace.usingAccessor = function replaceUsingAccessor(object, property, replacement) { + sandbox.replace.usingAccessor = function replaceUsingAccessor( + object, + property, + replacement + ) { const descriptor = getPropertyDescriptor(object, property); // checkForValidArguments(descriptor, property, replacement); // verifySameType(object, property, replacement); @@ -298,15 +299,12 @@ function Sandbox() { verifyNotReplaced(object, property); // store a function for restoring the replaced property - push( - fakeRestorers, - getFakeRestorer(object, property, true) - ); + push(fakeRestorers, getFakeRestorer(object, property, true)); object[property] = replacement; return replacement; - } + }; sandbox.define = function define(object, property, value) { const descriptor = getPropertyDescriptor(object, property); diff --git a/test/sandbox-test.js b/test/sandbox-test.js index 63ec93091..29aadae9a 100644 --- a/test/sandbox-test.js +++ b/test/sandbox-test.js @@ -1117,10 +1117,10 @@ describe("Sandbox", function () { }); }); - describe('.replace.usingAccessor', function () { + describe(".replace.usingAccessor", function () { it("should allow using assignment when replacing a value", function () { const sandbox = createSandbox(); - let quaziPrivateStateOfObject = "original"; + let quaziPrivateStateOfObject = "original"; const object = { // eslint-disable-next-line accessor-pairs get foo() { @@ -1136,7 +1136,7 @@ describe("Sandbox", function () { assert.equals(object.foo, "fake"); sandbox.restore(); assert.equals(object.foo, "original"); - }) + }); }); describe(".replaceGetter", function () {