From 387a6c61ebf95cbf485d3c1c8c89bcdadb4596a4 Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Mon, 7 Aug 2017 22:07:10 +0200 Subject: [PATCH 1/3] Remove check for deprecated method With release 1.0.0 we removed all uses of sinon.getConfig so it no longer makes sense to check for this method. This closes #77 --- lib/utils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 72d83c7..0c9770d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -14,6 +14,5 @@ exports.isPromise = function (object) { exports.isSinon = function (obj) { return !!obj && typeof obj === "object" && - typeof obj.getConfig === "function" && !!obj.sandbox && typeof obj.sandbox === "object" && typeof obj.sandbox.create === "function"; }; From 969a3a65e79b59f54990a26ec96675f8d97f4069 Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Mon, 7 Aug 2017 22:34:17 +0200 Subject: [PATCH 2/3] Run tests against Sinon 3 The test config now uses the new config object that arrived with Sinon 3. See http://sinonjs.org/releases/v3.0.0/fake-timers/ --- package.json | 4 ++-- test/test-test.js | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) 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..80220e7 100644 --- a/test/test-test.js +++ b/test/test-test.js @@ -772,7 +772,9 @@ module.exports = { var config = { injectIntoThis: false, properties: ["clock"], - useFakeTimers: ["Date", "setTimeout", "setImmediate"] + useFakeTimers: { + toFake: ["Date", "setTimeout", "setImmediate"] + } }; var testInstance = sinonTest(sinon, config); From fb0a3984d5825d5ff36f3b4a17001875ec79bd05 Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Mon, 7 Aug 2017 23:14:20 +0200 Subject: [PATCH 3/3] Use factory methods from Sinon 3.1+ Start using the factory method API available from Sinon 3.1, while keeping backwards compatibility. --- lib/test.js | 4 +++- lib/utils.js | 17 +++++++++++++++-- test/test-test.js | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) 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 0c9770d..7f6762b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,11 +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" && - !!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/test/test-test.js b/test/test-test.js index 80220e7..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;