diff --git a/lib/test.js b/lib/test.js index 8740301..486bad9 100644 --- a/lib/test.js +++ b/lib/test.js @@ -67,10 +67,12 @@ function configure(sinon, config) { throw new TypeError("expected sinon object"); } + var sandboxFactory = sinon.createSandbox || sinon.sandbox.create; + function callSandboxedFn(context, args, fn, handler) { config = getConfig(config); config.injectInto = config.injectIntoThis && context || config.injectInto; - var sandbox = sinon.sandbox.create(config); + var sandbox = sandboxFactory(config); var done = args.length && args[args.length - 1]; var result; diff --git a/lib/utils.js b/lib/utils.js index 72d83c7..7f6762b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,12 +8,24 @@ */ "use strict"; +/** + * From version 3.1 Sinon uses factory methods for sandboxes and deprecates + * sinon.sandbox. It - and its exports - will in time be removed/internalized, but + * we can still support backwards compatibility easily. + * See Sinon pull request #1515 +*/ +function isOlderSinonVersion(sinonObj) { + return typeof sinonObj.createSandbox === "undefined" + && !!sinonObj.sandbox + && typeof sinonObj.sandbox === "object" + && typeof sinonObj.sandbox.create === "function"; +} + exports.isPromise = function (object) { return typeof object === "object" && typeof object.then === "function"; }; exports.isSinon = function (obj) { - return !!obj && typeof obj === "object" && - typeof obj.getConfig === "function" && - !!obj.sandbox && typeof obj.sandbox === "object" && typeof obj.sandbox.create === "function"; + return !!obj && typeof obj === "object" + && (isOlderSinonVersion(obj) || typeof obj.createSandbox === "function"); }; diff --git a/package.json b/package.json index 0abec6a..309ecc5 100644 --- a/package.json +++ b/package.json @@ -39,10 +39,10 @@ "referee": "^1.2.0", "rollup": "^0.41.4", "rollup-plugin-commonjs": "^8.0.2", - "sinon": "2.x.x", + "sinon": "3.x.x", "uglify-js": "^3.0.7" }, "peerDependencies": { - "sinon": "^2.0.0" + "sinon": "2.x - 3.x" } } diff --git a/test/test-test.js b/test/test-test.js index 7f5ec00..331d96f 100644 --- a/test/test-test.js +++ b/test/test-test.js @@ -651,6 +651,28 @@ module.exports = { assert.mock(mocked); }, + "backwards compatibility": { + "uses the new factory methods introduced in 3.1 - if available": function () { + var fakeSinon = { createSandbox: sinon.stub() }; + var testInstance = sinonTest(fakeSinon); + + try { testInstance(function () {})(); } + catch (err) { /* ignore */ } + + assert(fakeSinon.createSandbox.called); + }, + + "falls back to the old 1.x-2.x methods when needed ": function () { + var fakeSinon = { sandbox: { create: sinon.stub() } }; + var testInstance = sinonTest(fakeSinon); + + try { testInstance(function () {})(); } + catch (err) { /* ignore */ } + + assert(fakeSinon.sandbox.create.called); + } + }, + "browser options": { "yields server when faking xhr": function () { var stubbed, mocked, server; @@ -772,7 +794,9 @@ module.exports = { var config = { injectIntoThis: false, properties: ["clock"], - useFakeTimers: ["Date", "setTimeout", "setImmediate"] + useFakeTimers: { + toFake: ["Date", "setTimeout", "setImmediate"] + } }; var testInstance = sinonTest(sinon, config);