Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatibility with Sinon 3 #78

Merged
merged 3 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
18 changes: 15 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
26 changes: 25 additions & 1 deletion test/test-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down