Skip to content

Commit 93db3ef

Browse files
authored
breaking: Remove sinon.defaultConfig and related modules (#2565)
* 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
1 parent 30e9e37 commit 93db3ef

File tree

6 files changed

+81
-163
lines changed

6 files changed

+81
-163
lines changed

docs/release-source/release/sandbox.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ const sandbox = sinon.createSandbox({
7979
##### `injectInto`
8080

8181
The sandbox's methods can be injected into another object for convenience. The
82-
`injectInto` configuration option can name an object to add properties to. See the example further down the page.
82+
`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.
83+
84+
See the example further down the page.
8385

8486
##### `properties`
8587

86-
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`.
88+
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`.
8789

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

121123
<div data-example-id="sandbox-configuration"></div>
122-
```
124+
125+
#### `inject(facadeObject)`
126+
127+
This is injects all the properties of the sandbox into the facade object.
128+
This is equivalent to specifying all the available properties in `properties` when you create a sandbox with `injectInto`.
123129

124130
##### `useFakeTimers`
125131

@@ -156,6 +162,12 @@ const sandbox = sinon.createSandbox({
156162
});
157163
```
158164

165+
Alternatively you can use the `sandbox.inject({})` method, which will inject all the sandbox methods by default, which is _usually_ what you want.
166+
167+
```javascript
168+
const myFacade = sandbox.inject({});
169+
```
170+
159171
#### `sandbox.assert();`
160172

161173
A convenience reference for [`sinon.assert`](./assertions)

lib/create-sinon-api.js

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ module.exports = function createApi(opts = { sinonXhrLib: nise }) {
2525
restoreObject: require("./sinon/restore-object"),
2626

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

3029
// fake timers
3130
timers: fakeTimers.timers,

lib/sinon/util/core/default-config.js

-21
This file was deleted.

test/get-config.js

-21
This file was deleted.

test/sandbox-test.js

+66-83
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const createSandbox = require("../lib/sinon/create-sandbox");
1414
const sinonFake = require("../lib/sinon/fake");
1515
const sinonSpy = require("../lib/sinon/spy");
1616
const sinonStub = require("../lib/sinon/stub");
17-
const sinonConfig = require("./get-config");
1817
const sinonClock = require("../lib/sinon/util/fake-timers");
1918

2019
const supportsAjax =
@@ -2053,11 +2052,9 @@ describe("Sandbox", function () {
20532052
});
20542053

20552054
it("yields stub, mock as arguments", function () {
2056-
const sandbox = createSandbox(
2057-
sinonConfig({
2058-
properties: ["stub", "mock"],
2059-
}),
2060-
);
2055+
const sandbox = createSandbox({
2056+
properties: ["stub", "mock"],
2057+
});
20612058

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

20692066
it("yields spy, stub, mock as arguments", function () {
2070-
const sandbox = createSandbox(
2071-
sinonConfig({
2072-
properties: ["spy", "stub", "mock"],
2073-
}),
2074-
);
2067+
const sandbox = createSandbox({
2068+
properties: ["spy", "stub", "mock"],
2069+
});
20752070

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

20832078
it("does not yield server when not faking xhr", function () {
2084-
const sandbox = createSandbox(
2085-
sinonConfig({
2086-
properties: ["server", "stub", "mock"],
2087-
useFakeServer: false,
2088-
}),
2089-
);
2079+
const sandbox = createSandbox({
2080+
properties: ["server", "stub", "mock"],
2081+
useFakeServer: false,
2082+
});
20902083

20912084
assert.equals(sandbox.args.length, 2);
20922085
assert.stub(sandbox.args[0]());
@@ -2102,12 +2095,10 @@ describe("Sandbox", function () {
21022095
const clock = {};
21032096
const spy = false;
21042097
const object = { server: server, clock: clock, spy: spy };
2105-
const sandbox = createSandbox(
2106-
sinonConfig({
2107-
properties: ["server", "clock", "spy"],
2108-
injectInto: object,
2109-
}),
2110-
);
2098+
const sandbox = createSandbox({
2099+
properties: ["server", "clock", "spy"],
2100+
injectInto: object,
2101+
});
21112102

21122103
assert.same(object.server, server);
21132104
assert.same(object.clock, clock);
@@ -2119,11 +2110,10 @@ describe("Sandbox", function () {
21192110
if (supportsAjax) {
21202111
describe("ajax options", function () {
21212112
it("yields server when faking xhr", function () {
2122-
const sandbox = createSandbox(
2123-
sinonConfig({
2124-
properties: ["server", "stub", "mock"],
2125-
}),
2126-
);
2113+
const sandbox = createSandbox({
2114+
useFakeServer: true,
2115+
properties: ["server", "stub", "mock"],
2116+
});
21272117

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

21362126
it("uses serverWithClock when faking xhr", function () {
2137-
const sandbox = createSandbox(
2138-
sinonConfig({
2139-
properties: ["server"],
2140-
useFakeServer: fakeServerWithClock,
2141-
}),
2142-
);
2127+
const sandbox = createSandbox({
2128+
properties: ["server"],
2129+
useFakeServer: fakeServerWithClock,
2130+
});
21432131

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

21662154
it("yields clock when faking timers", function () {
2167-
const sandbox = createSandbox(
2168-
sinonConfig({
2169-
properties: ["server", "clock"],
2170-
}),
2171-
);
2155+
const sandbox = createSandbox({
2156+
properties: ["server", "clock"],
2157+
useFakeServer: true,
2158+
useFakeTimers: true,
2159+
});
21722160

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

21762164
sandbox.restore();
21772165
});
21782166

2179-
it("injects properties into object", function () {
2167+
it("should inject server and clock when enabling them", function () {
21802168
const object = {};
21812169

2182-
const sandbox = createSandbox(
2183-
sinonConfig({
2184-
properties: ["server", "clock"],
2185-
injectInto: object,
2186-
}),
2187-
);
2170+
const sandbox = createSandbox({
2171+
injectInto: object,
2172+
properties: ["clock", "server", "requests"],
2173+
useFakeTimers: true,
2174+
useFakeServer: true,
2175+
});
21882176

21892177
assert.equals(sandbox.args.length, 0);
21902178
assert.equals(object.server, this.fakeServer);
21912179
assert.clock(object.clock);
2192-
assert.isUndefined(object.spy);
2193-
assert.isUndefined(object.stub);
2194-
assert.isUndefined(object.mock);
2195-
assert.isUndefined(object.requests);
2180+
assert.isArray(object.requests);
21962181

21972182
sandbox.restore();
21982183
});
21992184

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

2203-
const sandbox = createSandbox(
2204-
sinonConfig({
2205-
injectInto: object,
2206-
useFakeTimers: true,
2207-
useFakeServer: true,
2208-
}),
2209-
);
2188+
const sandbox = createSandbox({
2189+
injectInto: object,
2190+
properties: ["clock", "server", "requests"],
2191+
useFakeTimers: false,
2192+
useFakeServer: false,
2193+
});
22102194

2211-
assert.equals(sandbox.args.length, 0);
2212-
assert.equals(object.server, this.fakeServer);
2213-
assert.clock(object.clock);
2214-
assert.isFunction(object.spy);
2215-
assert.isFunction(object.stub);
2216-
assert.isFunction(object.mock);
2217-
assert.isArray(object.requests);
2195+
assert.isUndefined(object.requests);
22182196
assert.isUndefined(object.sandbox);
2197+
assert.isUndefined(object.clock);
22192198

22202199
sandbox.restore();
22212200
});
@@ -2225,12 +2204,10 @@ describe("Sandbox", function () {
22252204
// This is currently testing the internals of useFakeTimers, we could possibly change it to be based on
22262205
// behavior.
22272206
it("fakes specified timers", function () {
2228-
const sandbox = createSandbox(
2229-
sinonConfig({
2230-
properties: ["clock"],
2231-
useFakeTimers: { toFake: ["Date", "setTimeout"] },
2232-
}),
2233-
);
2207+
const sandbox = createSandbox({
2208+
properties: ["clock"],
2209+
useFakeTimers: { toFake: ["Date", "setTimeout"] },
2210+
});
22342211

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

2247-
const sandbox = createSandbox(
2248-
sinonConfig({
2249-
properties: ["sandbox", "spy"],
2250-
injectInto: object,
2251-
}),
2252-
);
2224+
const sandbox = createSandbox({
2225+
properties: ["sandbox", "spy"],
2226+
injectInto: object,
2227+
});
22532228

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

2264-
const sandbox = createSandbox(
2265-
sinonConfig({
2266-
properties: ["match"],
2267-
injectInto: object,
2268-
}),
2269-
);
2239+
const sandbox = createSandbox({
2240+
properties: ["match"],
2241+
injectInto: object,
2242+
});
22702243

22712244
assert.same(object.match, match);
22722245

22732246
sandbox.restore();
22742247
});
2248+
2249+
it("does not inject any properties by default", function () {
2250+
const object = {};
2251+
2252+
createSandbox({
2253+
injectInto: object,
2254+
});
2255+
2256+
assert.equals(Object.keys(object), []);
2257+
});
22752258
});
22762259

22772260
describe("getters and setters", function () {

test/util/core/get-config-test.js

-34
This file was deleted.

0 commit comments

Comments
 (0)