Skip to content

Commit

Permalink
Prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Aug 11, 2023
1 parent 91036eb commit 4dd185b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
15 changes: 11 additions & 4 deletions lib/sinon/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,26 +267,33 @@ function Sandbox() {

if (!options?.forceAssignment) {
if (typeof descriptor.get === "function") {
throw new Error("Use sandbox.replaceGetter for replacing getters");
throw new Error(
"Use sandbox.replaceGetter for replacing getters"
);
}

if (typeof descriptor.set === "function") {
throw new Error("Use sandbox.replaceSetter for replacing setters");
throw new Error(
"Use sandbox.replaceSetter for replacing setters"
);
}
}

if (typeof object[property] !== typeof replacement) {
throw new TypeError(
`Cannot replace ${typeof object[
property
]} with ${typeof replacement}`
]} with ${typeof replacement}`
);
}

verifyNotReplaced(object, property);

// store a function for restoring the replaced property
push(fakeRestorers, getFakeRestorer(object, property, options?.forceAssignment));
push(
fakeRestorers,
getFakeRestorer(object, property, options?.forceAssignment)
);

object[property] = replacement;

Expand Down
59 changes: 29 additions & 30 deletions test/sandbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("Sandbox", function () {
});

it("can be reset without failing when pre-configured to use a fake server", function () {
const sandbox = createSandbox({useFakeServer: true});
const sandbox = createSandbox({ useFakeServer: true });
refute.exception(function () {
sandbox.reset();
});
Expand Down Expand Up @@ -387,7 +387,7 @@ describe("Sandbox", function () {
foo: sinonStub().returns(3),
});
},
{message: "Cannot stub foo. Property does not exist!"}
{ message: "Cannot stub foo. Property does not exist!" }
);
});

Expand Down Expand Up @@ -564,7 +564,7 @@ describe("Sandbox", function () {

describe("stub anything", function () {
beforeEach(function () {
this.object = {property: 42};
this.object = { property: 42 };
this.sandbox = new Sandbox();
});

Expand Down Expand Up @@ -911,7 +911,7 @@ describe("Sandbox", function () {
function () {
sandbox.replace(object, "property", replacement);
},
{message: "Cannot replace string with function"}
{ message: "Cannot replace string with function" }
);
});

Expand All @@ -928,7 +928,7 @@ describe("Sandbox", function () {
function () {
sandbox.replace(object, "property", replacement);
},
{message: "Cannot replace function with string"}
{ message: "Cannot replace function with string" }
);
});

Expand Down Expand Up @@ -1032,23 +1032,25 @@ describe("Sandbox", function () {

it("should allow forcing assignment", function () {
const sandbox = this.sandbox;
let hiddenFoo = () => 'original';
let hiddenFoo = () => "original";
const object = {
// eslint-disable-next-line accessor-pairs
get foo(){
get foo() {
return hiddenFoo;
},
set foo(value) {
hiddenFoo = value;
},
};

assert.equals(object.foo(),'original')
sandbox.replace(object, "foo", sinonFake.returns('fake'), {forceAssignment: true});
assert.equals(object.foo(),'fake')
sandbox.restore()
assert.equals(object.foo(),'original');
})
assert.equals(object.foo(), "original");
sandbox.replace(object, "foo", sinonFake.returns("fake"), {
forceAssignment: true,
});
assert.equals(object.foo(), "fake");
sandbox.restore();
assert.equals(object.foo(), "original");
});
});
});

Expand Down Expand Up @@ -1430,8 +1432,8 @@ describe("Sandbox", function () {
const sandbox = (this.sandbox = createSandbox());
const fakes = sandbox.getFakes();

fakes.push({resetBehavior: sinonSpy()});
fakes.push({resetBehavior: sinonSpy()});
fakes.push({ resetBehavior: sinonSpy() });
fakes.push({ resetBehavior: sinonSpy() });
});

it("calls resetBehavior on all fakes", function () {
Expand Down Expand Up @@ -1517,13 +1519,13 @@ describe("Sandbox", function () {
"useFakeTimers"
).returns({});

this.sandbox.useFakeTimers({toFake: ["Date", "setTimeout"]});
this.sandbox.useFakeTimers({ toFake: ["Date", "setTimeout"] });
this.sandbox.useFakeTimers({
toFake: ["setTimeout", "clearTimeout", "setInterval"],
});

assert(
useFakeTimersStub.calledWith({toFake: ["Date", "setTimeout"]})
useFakeTimersStub.calledWith({ toFake: ["Date", "setTimeout"] })
);
assert(
useFakeTimersStub.calledWith({
Expand Down Expand Up @@ -1794,12 +1796,10 @@ describe("Sandbox", function () {
this.sandbox.inject(this.obj);

const myObj = {
a: function () {
}
a: function () {},
};

function MyClass() {
}
function MyClass() {}

MyClass.prototype.method1 = noop;
Object.defineProperty(myObj, "b", {
Expand All @@ -1809,8 +1809,7 @@ describe("Sandbox", function () {
configurable: true,
});
Object.defineProperty(myObj, "c", {
set: function () {
},
set: function () {},
configurable: true,
});

Expand Down Expand Up @@ -1896,8 +1895,8 @@ describe("Sandbox", function () {
const sandbox = createSandbox();
const fakes = sandbox.getFakes();

fakes.push({verify: sinonSpy()});
fakes.push({verify: sinonSpy()});
fakes.push({ verify: sinonSpy() });
fakes.push({ verify: sinonSpy() });

sandbox.verify();

Expand Down Expand Up @@ -1955,7 +1954,7 @@ describe("Sandbox", function () {
describe("configurable sandbox", function () {
beforeEach(function () {
this.requests = [];
this.fakeServer = {requests: this.requests};
this.fakeServer = { requests: this.requests };

this.useFakeTimersSpy = sinonSpy(sinonClock, "useFakeTimers");
sinonStub(fakeServer, "create").returns(this.fakeServer);
Expand Down Expand Up @@ -2015,7 +2014,7 @@ describe("Sandbox", function () {
};
const clock = {};
const spy = false;
const object = {server: server, clock: clock, spy: spy};
const object = { server: server, clock: clock, spy: spy };
const sandbox = createSandbox(
sinonConfig({
properties: ["server", "clock", "spy"],
Expand Down Expand Up @@ -2142,7 +2141,7 @@ describe("Sandbox", function () {
const sandbox = createSandbox(
sinonConfig({
properties: ["clock"],
useFakeTimers: {toFake: ["Date", "setTimeout"]},
useFakeTimers: { toFake: ["Date", "setTimeout"] },
})
);

Expand Down Expand Up @@ -2294,14 +2293,14 @@ describe("Sandbox", function () {
function () {
sandboxA.assert.fail("Some message");
},
{name: "CustomErrorA"}
{ name: "CustomErrorA" }
);

assert.exception(
function () {
sandboxB.assert.fail("Some message");
},
{name: "CustomErrorB"}
{ name: "CustomErrorB" }
);

sandboxA.restore();
Expand Down

0 comments on commit 4dd185b

Please sign in to comment.