diff --git a/docs/release-source/release/sandbox.md b/docs/release-source/release/sandbox.md index 7847a688b..f4d3c56d3 100644 --- a/docs/release-source/release/sandbox.md +++ b/docs/release-source/release/sandbox.md @@ -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`: @@ -119,7 +121,11 @@ and overflow your display. ```
-``` + +#### `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` @@ -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) diff --git a/lib/create-sinon-api.js b/lib/create-sinon-api.js index 9ef9ec126..58f0f93d1 100644 --- a/lib/create-sinon-api.js +++ b/lib/create-sinon-api.js @@ -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, diff --git a/lib/sinon/util/core/default-config.js b/lib/sinon/util/core/default-config.js deleted file mode 100644 index 60170b442..000000000 --- a/lib/sinon/util/core/default-config.js +++ /dev/null @@ -1,21 +0,0 @@ -"use strict"; - -module.exports = { - injectInto: null, - properties: [ - "spy", - "stub", - "mock", - "clock", - "server", - "requests", - "fake", - "define", - "replace", - "replaceSetter", - "replaceGetter", - "createStubInstance", - ], - useFakeTimers: true, - useFakeServer: true, -}; diff --git a/test/get-config.js b/test/get-config.js deleted file mode 100644 index 168af9069..000000000 --- a/test/get-config.js +++ /dev/null @@ -1,21 +0,0 @@ -"use strict"; - -const defaultConfig = require("../lib/sinon/util/core/default-config"); -const hasOwnProperty = - require("@sinonjs/commons").prototypes.object.hasOwnProperty; - -module.exports = function getConfig(custom) { - const config = {}; - let prop; - const kustom = custom || {}; - - for (prop in defaultConfig) { - if (hasOwnProperty(defaultConfig, prop)) { - config[prop] = hasOwnProperty(kustom, prop) - ? kustom[prop] - : defaultConfig[prop]; - } - } - - return config; -}; diff --git a/test/sandbox-test.js b/test/sandbox-test.js index 28b8f77c7..cc1818015 100644 --- a/test/sandbox-test.js +++ b/test/sandbox-test.js @@ -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 = @@ -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]()); @@ -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]()); @@ -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]()); @@ -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); @@ -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); @@ -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], @@ -2164,11 +2152,11 @@ 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]); @@ -2176,46 +2164,37 @@ describe("Sandbox", function () { 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(); }); @@ -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({ @@ -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); @@ -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 () { diff --git a/test/util/core/get-config-test.js b/test/util/core/get-config-test.js deleted file mode 100644 index 3586d3d05..000000000 --- a/test/util/core/get-config-test.js +++ /dev/null @@ -1,34 +0,0 @@ -"use strict"; - -const referee = require("@sinonjs/referee"); -const getConfig = require("../../get-config"); -const defaultConfig = require("../../../lib/sinon/util/core/default-config"); -const assert = referee.assert; -const refute = referee.refute; - -describe("core/util/getConfig", function () { - describe(".getConfig", function () { - it("gets copy of default config", function () { - const config = getConfig(); - - refute.same(config, defaultConfig); - assert.equals(config.injectInto, defaultConfig.injectInto); - assert.equals(config.properties, defaultConfig.properties); - assert.equals(config.useFakeTimers, defaultConfig.useFakeTimers); - assert.equals(config.useFakeServer, defaultConfig.useFakeServer); - }); - - it("should override specified properties", function () { - const config = getConfig({ - properties: ["stub", "mock"], - useFakeServer: false, - }); - - refute.same(config, defaultConfig); - assert.equals(config.injectInto, defaultConfig.injectInto); - assert.equals(config.properties, ["stub", "mock"]); - assert.equals(config.useFakeTimers, defaultConfig.useFakeTimers); - assert.isFalse(config.useFakeServer); - }); - }); -});