Skip to content

Commit

Permalink
Prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Oct 3, 2023
1 parent 8aa1581 commit 20b9061
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/release-source/release/sandbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);`
Expand Down
20 changes: 9 additions & 11 deletions lib/sinon/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function verifySameType(object, property, replacement) {
throw new TypeError(
`Cannot replace ${typeof object[
property
]} with ${typeof replacement}`
]} with ${typeof replacement}`
);
}
}
Expand Down Expand Up @@ -280,33 +280,31 @@ 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);

Check failure on line 295 in lib/sinon/sandbox.js

View workflow job for this annotation

GitHub Actions / lint

'descriptor' is assigned a value but never used
// checkForValidArguments(descriptor, property, replacement);
// verifySameType(object, property, replacement);

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);
Expand Down
6 changes: 3 additions & 3 deletions test/sandbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -1136,7 +1136,7 @@ describe("Sandbox", function () {
assert.equals(object.foo, "fake");
sandbox.restore();
assert.equals(object.foo, "original");
})
});
});

describe(".replaceGetter", function () {
Expand Down

0 comments on commit 20b9061

Please sign in to comment.