Skip to content

Commit

Permalink
breaking: Remove sinon.defaultConfig and related modules (#2565)
Browse files Browse the repository at this point in the history
* breaking: Remove sinon.defaultConfig and related modules

default-config and get-config are leftovers from when Sinon
shipped with sinon.test (now the independent NPM module
 'sinon-test').

These serve no purpose internally, and really have no purpose
but to help sinon-test create a base default. If needed,
these can be copied into the sinon-test project. No projects
should depend on these (my assumption), but since it is a
change of the API we mark it as a breaking change

fixes #2561

* fixed up tests that were lying

It seemed like the the 'injectInto' option
would expose most props by default. This was
not the case. That was formerly hidden by
using the getConfig call that added props
that were never used in the actual implementation.

Added another test to make this more explicit.
Will add docs on this.

* Document Sandbox#inject

This was added in Sinon 0.6 but has never been documented

* Note that 'properties' is empty by default
  • Loading branch information
fatso83 authored Nov 2, 2023
1 parent 30e9e37 commit 93db3ef
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 163 deletions.
18 changes: 15 additions & 3 deletions docs/release-source/release/sandbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ const sandbox = sinon.createSandbox({
##### `injectInto`

The sandbox's methods can be injected into another object for convenience. The
`injectInto` configuration option can name an object to add properties to. See the example further down the page.
`injectInto` configuration option can name an object to add properties to. Note that you explicitly need to specify all the properties you want to expose using the `properties` field.

See the example further down the page.

##### `properties`

Which properties to inject into the facade object. Note that only naming "server" here is not sufficient to have a `server` property show up in the target object, you also have to set `useFakeServer` to `true`.
Which properties to inject into the facade object. By default empty! Note that only naming "server" here is not sufficient to have a `server` property show up in the target object, you also have to set `useFakeServer` to `true`.

The list of properties that can be injected are the ones exposed by the object
returned by the function `inject`:
Expand Down Expand Up @@ -119,7 +121,11 @@ and overflow your display.
```

<div data-example-id="sandbox-configuration"></div>
```

#### `inject(facadeObject)`

This is injects all the properties of the sandbox into the facade object.
This is equivalent to specifying all the available properties in `properties` when you create a sandbox with `injectInto`.

##### `useFakeTimers`

Expand Down Expand Up @@ -156,6 +162,12 @@ const sandbox = sinon.createSandbox({
});
```

Alternatively you can use the `sandbox.inject({})` method, which will inject all the sandbox methods by default, which is _usually_ what you want.

```javascript
const myFacade = sandbox.inject({});
```

#### `sandbox.assert();`

A convenience reference for [`sinon.assert`](./assertions)
Expand Down
1 change: 0 additions & 1 deletion lib/create-sinon-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module.exports = function createApi(opts = { sinonXhrLib: nise }) {
restoreObject: require("./sinon/restore-object"),

expectation: require("./sinon/mock-expectation"),
defaultConfig: require("./sinon/util/core/default-config"),

// fake timers
timers: fakeTimers.timers,
Expand Down
21 changes: 0 additions & 21 deletions lib/sinon/util/core/default-config.js

This file was deleted.

21 changes: 0 additions & 21 deletions test/get-config.js

This file was deleted.

149 changes: 66 additions & 83 deletions test/sandbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const createSandbox = require("../lib/sinon/create-sandbox");
const sinonFake = require("../lib/sinon/fake");
const sinonSpy = require("../lib/sinon/spy");
const sinonStub = require("../lib/sinon/stub");
const sinonConfig = require("./get-config");
const sinonClock = require("../lib/sinon/util/fake-timers");

const supportsAjax =
Expand Down Expand Up @@ -2053,11 +2052,9 @@ describe("Sandbox", function () {
});

it("yields stub, mock as arguments", function () {
const sandbox = createSandbox(
sinonConfig({
properties: ["stub", "mock"],
}),
);
const sandbox = createSandbox({
properties: ["stub", "mock"],
});

assert.equals(sandbox.args.length, 2);
assert.stub(sandbox.args[0]());
Expand All @@ -2067,11 +2064,9 @@ describe("Sandbox", function () {
});

it("yields spy, stub, mock as arguments", function () {
const sandbox = createSandbox(
sinonConfig({
properties: ["spy", "stub", "mock"],
}),
);
const sandbox = createSandbox({
properties: ["spy", "stub", "mock"],
});

assert.spy(sandbox.args[0]());
assert.stub(sandbox.args[1]());
Expand All @@ -2081,12 +2076,10 @@ describe("Sandbox", function () {
});

it("does not yield server when not faking xhr", function () {
const sandbox = createSandbox(
sinonConfig({
properties: ["server", "stub", "mock"],
useFakeServer: false,
}),
);
const sandbox = createSandbox({
properties: ["server", "stub", "mock"],
useFakeServer: false,
});

assert.equals(sandbox.args.length, 2);
assert.stub(sandbox.args[0]());
Expand All @@ -2102,12 +2095,10 @@ describe("Sandbox", function () {
const clock = {};
const spy = false;
const object = { server: server, clock: clock, spy: spy };
const sandbox = createSandbox(
sinonConfig({
properties: ["server", "clock", "spy"],
injectInto: object,
}),
);
const sandbox = createSandbox({
properties: ["server", "clock", "spy"],
injectInto: object,
});

assert.same(object.server, server);
assert.same(object.clock, clock);
Expand All @@ -2119,11 +2110,10 @@ describe("Sandbox", function () {
if (supportsAjax) {
describe("ajax options", function () {
it("yields server when faking xhr", function () {
const sandbox = createSandbox(
sinonConfig({
properties: ["server", "stub", "mock"],
}),
);
const sandbox = createSandbox({
useFakeServer: true,
properties: ["server", "stub", "mock"],
});

assert.equals(sandbox.args.length, 3);
assert.equals(sandbox.args[0], this.fakeServer);
Expand All @@ -2134,12 +2124,10 @@ describe("Sandbox", function () {
});

it("uses serverWithClock when faking xhr", function () {
const sandbox = createSandbox(
sinonConfig({
properties: ["server"],
useFakeServer: fakeServerWithClock,
}),
);
const sandbox = createSandbox({
properties: ["server"],
useFakeServer: fakeServerWithClock,
});

assert.fakeServerWithClock(
sandbox.args[0],
Expand All @@ -2164,58 +2152,49 @@ describe("Sandbox", function () {
});

it("yields clock when faking timers", function () {
const sandbox = createSandbox(
sinonConfig({
properties: ["server", "clock"],
}),
);
const sandbox = createSandbox({
properties: ["server", "clock"],
useFakeServer: true,
useFakeTimers: true,
});

assert.same(sandbox.args[0], this.fakeServer);
assert.clock(sandbox.args[1]);

sandbox.restore();
});

it("injects properties into object", function () {
it("should inject server and clock when enabling them", function () {
const object = {};

const sandbox = createSandbox(
sinonConfig({
properties: ["server", "clock"],
injectInto: object,
}),
);
const sandbox = createSandbox({
injectInto: object,
properties: ["clock", "server", "requests"],
useFakeTimers: true,
useFakeServer: true,
});

assert.equals(sandbox.args.length, 0);
assert.equals(object.server, this.fakeServer);
assert.clock(object.clock);
assert.isUndefined(object.spy);
assert.isUndefined(object.stub);
assert.isUndefined(object.mock);
assert.isUndefined(object.requests);
assert.isArray(object.requests);

sandbox.restore();
});

it("should inject server and clock when only enabling them", function () {
it("should not inject server and clock if not enabled, even if the props are whitelisted", function () {
const object = {};

const sandbox = createSandbox(
sinonConfig({
injectInto: object,
useFakeTimers: true,
useFakeServer: true,
}),
);
const sandbox = createSandbox({
injectInto: object,
properties: ["clock", "server", "requests"],
useFakeTimers: false,
useFakeServer: false,
});

assert.equals(sandbox.args.length, 0);
assert.equals(object.server, this.fakeServer);
assert.clock(object.clock);
assert.isFunction(object.spy);
assert.isFunction(object.stub);
assert.isFunction(object.mock);
assert.isArray(object.requests);
assert.isUndefined(object.requests);
assert.isUndefined(object.sandbox);
assert.isUndefined(object.clock);

sandbox.restore();
});
Expand All @@ -2225,12 +2204,10 @@ describe("Sandbox", function () {
// This is currently testing the internals of useFakeTimers, we could possibly change it to be based on
// behavior.
it("fakes specified timers", function () {
const sandbox = createSandbox(
sinonConfig({
properties: ["clock"],
useFakeTimers: { toFake: ["Date", "setTimeout"] },
}),
);
const sandbox = createSandbox({
properties: ["clock"],
useFakeTimers: { toFake: ["Date", "setTimeout"] },
});

assert(
this.useFakeTimersSpy.calledWith({
Expand All @@ -2244,12 +2221,10 @@ describe("Sandbox", function () {
it("injects sandbox", function () {
const object = {};

const sandbox = createSandbox(
sinonConfig({
properties: ["sandbox", "spy"],
injectInto: object,
}),
);
const sandbox = createSandbox({
properties: ["sandbox", "spy"],
injectInto: object,
});

assert.equals(sandbox.args.length, 0);
assert.isFunction(object.spy);
Expand All @@ -2261,17 +2236,25 @@ describe("Sandbox", function () {
it("injects match", function () {
const object = {};

const sandbox = createSandbox(
sinonConfig({
properties: ["match"],
injectInto: object,
}),
);
const sandbox = createSandbox({
properties: ["match"],
injectInto: object,
});

assert.same(object.match, match);

sandbox.restore();
});

it("does not inject any properties by default", function () {
const object = {};

createSandbox({
injectInto: object,
});

assert.equals(Object.keys(object), []);
});
});

describe("getters and setters", function () {
Expand Down
34 changes: 0 additions & 34 deletions test/util/core/get-config-test.js

This file was deleted.

0 comments on commit 93db3ef

Please sign in to comment.