sinon-test
Advanced tools
Comparing version 2.4.0 to 2.4.1
@@ -15,17 +15,22 @@ /** | ||
* See Sinon pull request #1515 | ||
*/ | ||
*/ | ||
function isOlderSinonVersion(sinonObj) { | ||
return typeof sinonObj.createSandbox === "undefined" | ||
&& !!sinonObj.sandbox | ||
&& typeof sinonObj.sandbox === "object" | ||
&& typeof sinonObj.sandbox.create === "function"; | ||
return ( | ||
typeof sinonObj.createSandbox === "undefined" && | ||
Boolean(sinonObj.sandbox) && | ||
typeof sinonObj.sandbox === "object" && | ||
typeof sinonObj.sandbox.create === "function" | ||
); | ||
} | ||
var isPromise = function (object) { | ||
var isPromise = function(object) { | ||
return typeof object === "object" && typeof object.then === "function"; | ||
}; | ||
var isSinon = function (obj) { | ||
return !!obj && typeof obj === "object" | ||
&& (isOlderSinonVersion(obj) || typeof obj.createSandbox === "function"); | ||
var isSinon = function(obj) { | ||
var isObject = Boolean(obj) && typeof obj === "object"; | ||
return ( | ||
isObject && | ||
(isOlderSinonVersion(obj) || typeof obj.createSandbox === "function") | ||
); | ||
}; | ||
@@ -38,19 +43,13 @@ | ||
var defaultConfig = { | ||
injectIntoThis: true, | ||
injectInto: null, | ||
properties: ["spy", "stub", "mock", "clock", "server", "requests"], | ||
useFakeTimers: true, | ||
useFakeServer: true | ||
}; | ||
var getConfig = function getConfig(custom) { | ||
var getConfig = function getConfig(defaultConfig, custom) { | ||
var config = {}; | ||
var prop; | ||
custom = custom || {}; | ||
var internalCustom = custom || {}; | ||
for (prop in defaultConfig) { | ||
if (defaultConfig.hasOwnProperty(prop)) { | ||
config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaultConfig[prop]; | ||
config[prop] = internalCustom.hasOwnProperty(prop) | ||
? internalCustom[prop] | ||
: defaultConfig[prop]; | ||
} | ||
@@ -102,10 +101,14 @@ } | ||
if (result && utils.isPromise(result)) { | ||
if (!isAsync) { // # issue #75 | ||
if (!isAsync) { | ||
// # issue #75 | ||
return handleFn(sandbox, result); | ||
} | ||
finish(sandbox, new Error( | ||
"Your test should take a callback *or* return a promise. " | ||
+ "It should not do both." | ||
)); | ||
finish( | ||
sandbox, | ||
new Error( | ||
"Your test should take a callback *or* return a promise. " + | ||
"It should not do both." | ||
) | ||
); | ||
} | ||
@@ -128,6 +131,12 @@ | ||
var defaultConfig = Object.assign( | ||
{ injectIntoThis: true }, | ||
sinon.defaultConfig | ||
); | ||
function callSandboxedFn(context, args, fn, handler) { | ||
config = getConfig(config); | ||
config.injectInto = config.injectIntoThis && context || config.injectInto; | ||
var sandbox = sandboxFactory(config); | ||
var internalConfig = getConfig(defaultConfig, config); | ||
internalConfig.injectInto = | ||
(internalConfig.injectIntoThis && context) || | ||
internalConfig.injectInto; | ||
var sandbox = sandboxFactory(internalConfig); | ||
var done = args.length && args[args.length - 1]; | ||
@@ -156,13 +165,25 @@ var result; | ||
if (type !== "function") { | ||
throw new TypeError("sinon.test needs to wrap a test function, got " + type); | ||
throw new TypeError( | ||
"sinon.test needs to wrap a test function, got " + type | ||
); | ||
} | ||
return callback.length | ||
? function sinonAsyncSandboxedTest(_) { // eslint-disable-line no-unused-vars | ||
return callSandboxedFn(this, slice.call(arguments), callback, handleAsyncFn); | ||
} | ||
? // eslint-disable-next-line no-unused-vars | ||
function sinonAsyncSandboxedTest(_) { | ||
return callSandboxedFn( | ||
this, | ||
slice.call(arguments), | ||
callback, | ||
handleAsyncFn | ||
); | ||
} | ||
: function sinonSandboxedTest() { | ||
return callSandboxedFn(this, slice.call(arguments), callback, handleFn); | ||
} | ||
; | ||
return callSandboxedFn( | ||
this, | ||
slice.call(arguments), | ||
callback, | ||
handleFn | ||
); | ||
}; | ||
}; | ||
@@ -180,3 +201,3 @@ } | ||
} | ||
sinonTest.configureTest = function (sinon, config) { | ||
sinonTest.configureTest = function(sinon, config) { | ||
console.log("sinonTest.configureTest is deprecated and will be removed from the public API in a future version of sinon-test"); // eslint-disable-line | ||
@@ -183,0 +204,0 @@ return test.configure(sinon, config); |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.sinonTest = factory()); | ||
(global = global || self, global.sinonTest = factory()); | ||
}(this, (function () { 'use strict'; | ||
@@ -21,17 +21,22 @@ | ||
* See Sinon pull request #1515 | ||
*/ | ||
*/ | ||
function isOlderSinonVersion(sinonObj) { | ||
return typeof sinonObj.createSandbox === "undefined" | ||
&& !!sinonObj.sandbox | ||
&& typeof sinonObj.sandbox === "object" | ||
&& typeof sinonObj.sandbox.create === "function"; | ||
return ( | ||
typeof sinonObj.createSandbox === "undefined" && | ||
Boolean(sinonObj.sandbox) && | ||
typeof sinonObj.sandbox === "object" && | ||
typeof sinonObj.sandbox.create === "function" | ||
); | ||
} | ||
var isPromise = function (object) { | ||
var isPromise = function(object) { | ||
return typeof object === "object" && typeof object.then === "function"; | ||
}; | ||
var isSinon = function (obj) { | ||
return !!obj && typeof obj === "object" | ||
&& (isOlderSinonVersion(obj) || typeof obj.createSandbox === "function"); | ||
var isSinon = function(obj) { | ||
var isObject = Boolean(obj) && typeof obj === "object"; | ||
return ( | ||
isObject && | ||
(isOlderSinonVersion(obj) || typeof obj.createSandbox === "function") | ||
); | ||
}; | ||
@@ -44,19 +49,13 @@ | ||
var defaultConfig = { | ||
injectIntoThis: true, | ||
injectInto: null, | ||
properties: ["spy", "stub", "mock", "clock", "server", "requests"], | ||
useFakeTimers: true, | ||
useFakeServer: true | ||
}; | ||
var getConfig = function getConfig(custom) { | ||
var getConfig = function getConfig(defaultConfig, custom) { | ||
var config = {}; | ||
var prop; | ||
custom = custom || {}; | ||
var internalCustom = custom || {}; | ||
for (prop in defaultConfig) { | ||
if (defaultConfig.hasOwnProperty(prop)) { | ||
config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaultConfig[prop]; | ||
config[prop] = internalCustom.hasOwnProperty(prop) | ||
? internalCustom[prop] | ||
: defaultConfig[prop]; | ||
} | ||
@@ -108,10 +107,14 @@ } | ||
if (result && utils.isPromise(result)) { | ||
if (!isAsync) { // # issue #75 | ||
if (!isAsync) { | ||
// # issue #75 | ||
return handleFn(sandbox, result); | ||
} | ||
finish(sandbox, new Error( | ||
"Your test should take a callback *or* return a promise. " | ||
+ "It should not do both." | ||
)); | ||
finish( | ||
sandbox, | ||
new Error( | ||
"Your test should take a callback *or* return a promise. " + | ||
"It should not do both." | ||
) | ||
); | ||
} | ||
@@ -134,6 +137,12 @@ | ||
var defaultConfig = Object.assign( | ||
{ injectIntoThis: true }, | ||
sinon.defaultConfig | ||
); | ||
function callSandboxedFn(context, args, fn, handler) { | ||
config = getConfig(config); | ||
config.injectInto = config.injectIntoThis && context || config.injectInto; | ||
var sandbox = sandboxFactory(config); | ||
var internalConfig = getConfig(defaultConfig, config); | ||
internalConfig.injectInto = | ||
(internalConfig.injectIntoThis && context) || | ||
internalConfig.injectInto; | ||
var sandbox = sandboxFactory(internalConfig); | ||
var done = args.length && args[args.length - 1]; | ||
@@ -162,13 +171,25 @@ var result; | ||
if (type !== "function") { | ||
throw new TypeError("sinon.test needs to wrap a test function, got " + type); | ||
throw new TypeError( | ||
"sinon.test needs to wrap a test function, got " + type | ||
); | ||
} | ||
return callback.length | ||
? function sinonAsyncSandboxedTest(_) { // eslint-disable-line no-unused-vars | ||
return callSandboxedFn(this, slice.call(arguments), callback, handleAsyncFn); | ||
} | ||
? // eslint-disable-next-line no-unused-vars | ||
function sinonAsyncSandboxedTest(_) { | ||
return callSandboxedFn( | ||
this, | ||
slice.call(arguments), | ||
callback, | ||
handleAsyncFn | ||
); | ||
} | ||
: function sinonSandboxedTest() { | ||
return callSandboxedFn(this, slice.call(arguments), callback, handleFn); | ||
} | ||
; | ||
return callSandboxedFn( | ||
this, | ||
slice.call(arguments), | ||
callback, | ||
handleFn | ||
); | ||
}; | ||
}; | ||
@@ -186,3 +207,3 @@ } | ||
} | ||
sinonTest.configureTest = function (sinon, config) { | ||
sinonTest.configureTest = function(sinon, config) { | ||
console.log("sinonTest.configureTest is deprecated and will be removed from the public API in a future version of sinon-test"); // eslint-disable-line | ||
@@ -189,0 +210,0 @@ return test.configure(sinon, config); |
2.4.1 / 2019-12-03 | ||
================== | ||
* Reintroduce injectIntoThis in local config (#135) making it work with Sinon 7.3+ | ||
2.4.0 / 2018-10-19 | ||
@@ -3,0 +8,0 @@ ================== |
"use strict"; | ||
var defaultConfig = require("./default-config"); | ||
module.exports = function getConfig(custom) { | ||
module.exports = function getConfig(defaultConfig, custom) { | ||
var config = {}; | ||
var prop; | ||
custom = custom || {}; | ||
var internalCustom = custom || {}; | ||
for (prop in defaultConfig) { | ||
if (defaultConfig.hasOwnProperty(prop)) { | ||
config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaultConfig[prop]; | ||
config[prop] = internalCustom.hasOwnProperty(prop) | ||
? internalCustom[prop] | ||
: defaultConfig[prop]; | ||
} | ||
@@ -15,0 +15,0 @@ } |
@@ -16,3 +16,3 @@ /** | ||
} | ||
sinonTest.configureTest = function (sinon, config) { | ||
sinonTest.configureTest = function(sinon, config) { | ||
console.log("sinonTest.configureTest is deprecated and will be removed from the public API in a future version of sinon-test"); // eslint-disable-line | ||
@@ -19,0 +19,0 @@ return test.configure(sinon, config); |
@@ -53,10 +53,14 @@ /** | ||
if (result && utils.isPromise(result)) { | ||
if (!isAsync) { // # issue #75 | ||
if (!isAsync) { | ||
// # issue #75 | ||
return handleFn(sandbox, result); | ||
} | ||
finish(sandbox, new Error( | ||
"Your test should take a callback *or* return a promise. " | ||
+ "It should not do both." | ||
)); | ||
finish( | ||
sandbox, | ||
new Error( | ||
"Your test should take a callback *or* return a promise. " + | ||
"It should not do both." | ||
) | ||
); | ||
} | ||
@@ -79,6 +83,12 @@ | ||
var defaultConfig = Object.assign( | ||
{ injectIntoThis: true }, | ||
sinon.defaultConfig | ||
); | ||
function callSandboxedFn(context, args, fn, handler) { | ||
config = getConfig(config); | ||
config.injectInto = config.injectIntoThis && context || config.injectInto; | ||
var sandbox = sandboxFactory(config); | ||
var internalConfig = getConfig(defaultConfig, config); | ||
internalConfig.injectInto = | ||
(internalConfig.injectIntoThis && context) || | ||
internalConfig.injectInto; | ||
var sandbox = sandboxFactory(internalConfig); | ||
var done = args.length && args[args.length - 1]; | ||
@@ -107,13 +117,25 @@ var result; | ||
if (type !== "function") { | ||
throw new TypeError("sinon.test needs to wrap a test function, got " + type); | ||
throw new TypeError( | ||
"sinon.test needs to wrap a test function, got " + type | ||
); | ||
} | ||
return callback.length | ||
? function sinonAsyncSandboxedTest(_) { // eslint-disable-line no-unused-vars | ||
return callSandboxedFn(this, slice.call(arguments), callback, handleAsyncFn); | ||
} | ||
? // eslint-disable-next-line no-unused-vars | ||
function sinonAsyncSandboxedTest(_) { | ||
return callSandboxedFn( | ||
this, | ||
slice.call(arguments), | ||
callback, | ||
handleAsyncFn | ||
); | ||
} | ||
: function sinonSandboxedTest() { | ||
return callSandboxedFn(this, slice.call(arguments), callback, handleFn); | ||
} | ||
; | ||
return callSandboxedFn( | ||
this, | ||
slice.call(arguments), | ||
callback, | ||
handleFn | ||
); | ||
}; | ||
}; | ||
@@ -120,0 +142,0 @@ } |
@@ -16,17 +16,22 @@ /** | ||
* See Sinon pull request #1515 | ||
*/ | ||
*/ | ||
function isOlderSinonVersion(sinonObj) { | ||
return typeof sinonObj.createSandbox === "undefined" | ||
&& !!sinonObj.sandbox | ||
&& typeof sinonObj.sandbox === "object" | ||
&& typeof sinonObj.sandbox.create === "function"; | ||
return ( | ||
typeof sinonObj.createSandbox === "undefined" && | ||
Boolean(sinonObj.sandbox) && | ||
typeof sinonObj.sandbox === "object" && | ||
typeof sinonObj.sandbox.create === "function" | ||
); | ||
} | ||
exports.isPromise = function (object) { | ||
exports.isPromise = function(object) { | ||
return typeof object === "object" && typeof object.then === "function"; | ||
}; | ||
exports.isSinon = function (obj) { | ||
return !!obj && typeof obj === "object" | ||
&& (isOlderSinonVersion(obj) || typeof obj.createSandbox === "function"); | ||
exports.isSinon = function(obj) { | ||
var isObject = Boolean(obj) && typeof obj === "object"; | ||
return ( | ||
isObject && | ||
(isOlderSinonVersion(obj) || typeof obj.createSandbox === "function") | ||
); | ||
}; |
{ | ||
"name": "sinon-test", | ||
"version": "2.4.0", | ||
"version": "2.4.1", | ||
"description": "", | ||
@@ -12,4 +12,4 @@ "main": "lib/index.js", | ||
"build:dist-folder": "mkdirp dist", | ||
"coverage": "nyc report --reporter text-lcov | coveralls", | ||
"test": "nyc mocha", | ||
"test": "mocha", | ||
"test-coverage": "nyc --reporter text --reporter html --reporter lcovonly npm run test", | ||
"//integration-test": "This cannot run as a prepare step, as it will trigger a loop", | ||
@@ -40,16 +40,19 @@ "integration-test": "test-repos/do-test.sh", | ||
"devDependencies": { | ||
"@sinonjs/referee": "^2.1.1", | ||
"@sinonjs/referee": "^3.2.0", | ||
"es6-promise": "^4.2.4", | ||
"eslint": "^4.19.1", | ||
"eslint-config-sinon": "^1.0.3", | ||
"eslint": "^6.5.1", | ||
"eslint-config-prettier": "^6.3.0", | ||
"eslint-config-sinon": "^3.0.1", | ||
"eslint-plugin-ie11": "^1.0.0", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^5.2.0", | ||
"mocha": "^6.2.1", | ||
"npm-run-all": "^4.1.3", | ||
"nyc": "^12.0.2", | ||
"nyc": "^14.1.1", | ||
"phantomjs-prebuilt": "^2.1.16", | ||
"pre-commit": "^1.2.2", | ||
"rollup": "^0.60.0", | ||
"rollup-plugin-commonjs": "^9.1.3", | ||
"sinon": "^7.0.0" | ||
"prettier": "^1.18.2", | ||
"rollup": "^1.23.1", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"sinon": "^7.5.0" | ||
}, | ||
@@ -56,0 +59,0 @@ "peerDependencies": { |
@@ -46,4 +46,3 @@ # Sinon Test | ||
Do notice that | ||
we use a `function` and not a arrow function (ES2015) | ||
Do notice that we use a `function` and not a arrow function (ES2015) | ||
when wrapping the test with `sinon.test` as it needs | ||
@@ -126,3 +125,3 @@ to be able to access the `this` pointer used inside | ||
In order to [configure the sandbox](http://sinonjs.org/releases/v2.3.5/sandbox/) that is created, a configuration hash can be passed as a 2nd argument to `sinonTest`: | ||
In order to [configure the sandbox](http://sinonjs.org/releases/latest/sandbox#var-sandbox--sinoncreatesandboxconfig) that is created, a configuration hash can be passed as a 2nd argument to `sinonTest`: | ||
@@ -133,2 +132,4 @@ ```js | ||
The only difference to the standard configuration object for Sinon's sandbox is the addition of the `injectIntoThis` property, which is used to inject the sandbox' props into the context object (`this`). | ||
### Backwards compatibility | ||
@@ -135,0 +136,0 @@ Sinon 1.x used to ship with this functionality built-in, exposed as `sinon.test()`. You can keep all your existing test code by configuring an instance of `sinon-test`, as done above, and then assigning it to `sinon` like this in your tests: |
26486
525
138
17
10