Socket
Socket
Sign inDemoInstall

sinon

Package Overview
Dependencies
Maintainers
4
Versions
206
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sinon - npm Package Compare versions

Comparing version 9.2.0 to 9.2.1

8

CHANGELOG.md
9.2.1 / 2020-10-28
==================
* Fix #2203: skip writing 'name' property if not writable (#2304)
* Update error message on assert when representation of expected and actual value is equal, fixing issue #2084 (#2303)
* Make sandboxes each use their own assert object (#2302)
* Add usingPromise() method on fakes to fix issue #2293 (#2301)
9.2.0 / 2020-10-06

@@ -3,0 +11,0 @@ ==================

335

lib/sinon/assert.js

@@ -18,201 +18,208 @@ "use strict";

var assert;
function createAssertObject() {
var assert;
function verifyIsStub() {
var args = arraySlice(arguments);
function verifyIsStub() {
var args = arraySlice(arguments);
forEach(args, function(method) {
if (!method) {
assert.fail("fake is not a spy");
}
if (method.proxy && method.proxy.isSinonProxy) {
verifyIsStub(method.proxy);
} else {
if (typeof method !== "function") {
assert.fail(method + " is not a function");
forEach(args, function(method) {
if (!method) {
assert.fail("fake is not a spy");
}
if (typeof method.getCall !== "function") {
assert.fail(method + " is not stubbed");
}
}
});
}
if (method.proxy && method.proxy.isSinonProxy) {
verifyIsStub(method.proxy);
} else {
if (typeof method !== "function") {
assert.fail(method + " is not a function");
}
function verifyIsValidAssertion(assertionMethod, assertionArgs) {
switch (assertionMethod) {
case "notCalled":
case "called":
case "calledOnce":
case "calledTwice":
case "calledThrice":
if (assertionArgs.length !== 0) {
assert.fail(
assertionMethod +
" takes 1 argument but was called with " +
(assertionArgs.length + 1) +
" arguments"
);
if (typeof method.getCall !== "function") {
assert.fail(method + " is not stubbed");
}
}
break;
default:
break;
});
}
}
function failAssertion(object, msg) {
var obj = object || globalObject;
var failMethod = obj.fail || assert.fail;
failMethod.call(obj, msg);
}
function verifyIsValidAssertion(assertionMethod, assertionArgs) {
switch (assertionMethod) {
case "notCalled":
case "called":
case "calledOnce":
case "calledTwice":
case "calledThrice":
if (assertionArgs.length !== 0) {
assert.fail(
assertionMethod +
" takes 1 argument but was called with " +
(assertionArgs.length + 1) +
" arguments"
);
}
break;
default:
break;
}
}
function mirrorPropAsAssertion(name, method, message) {
var msg = message;
var meth = method;
if (arguments.length === 2) {
msg = method;
meth = name;
function failAssertion(object, msg) {
var obj = object || globalObject;
var failMethod = obj.fail || assert.fail;
failMethod.call(obj, msg);
}
assert[name] = function(fake) {
verifyIsStub(fake);
function mirrorPropAsAssertion(name, method, message) {
var msg = message;
var meth = method;
if (arguments.length === 2) {
msg = method;
meth = name;
}
var args = arraySlice(arguments, 1);
var failed = false;
assert[name] = function(fake) {
verifyIsStub(fake);
verifyIsValidAssertion(name, args);
var args = arraySlice(arguments, 1);
var failed = false;
if (typeof meth === "function") {
failed = !meth(fake);
} else {
failed = typeof fake[meth] === "function" ? !fake[meth].apply(fake, args) : !fake[meth];
}
verifyIsValidAssertion(name, args);
if (failed) {
failAssertion(this, (fake.printf || fake.proxy.printf).apply(fake, concat([msg], args)));
} else {
assert.pass(name);
}
};
}
if (typeof meth === "function") {
failed = !meth(fake);
} else {
failed = typeof fake[meth] === "function" ? !fake[meth].apply(fake, args) : !fake[meth];
}
function exposedName(prefix, prop) {
return !prefix || /^fail/.test(prop) ? prop : prefix + stringSlice(prop, 0, 1).toUpperCase() + stringSlice(prop, 1);
}
if (failed) {
failAssertion(this, (fake.printf || fake.proxy.printf).apply(fake, concat([msg], args)));
} else {
assert.pass(name);
}
};
}
assert = {
failException: "AssertError",
function exposedName(prefix, prop) {
return !prefix || /^fail/.test(prop)
? prop
: prefix + stringSlice(prop, 0, 1).toUpperCase() + stringSlice(prop, 1);
}
fail: function fail(message) {
var error = new Error(message);
error.name = this.failException || assert.failException;
assert = {
failException: "AssertError",
throw error;
},
fail: function fail(message) {
var error = new Error(message);
error.name = this.failException || assert.failException;
pass: function pass() {
return;
},
throw error;
},
callOrder: function assertCallOrder() {
verifyIsStub.apply(null, arguments);
var expected = "";
var actual = "";
pass: function pass() {
return;
},
if (!calledInOrder(arguments)) {
try {
expected = join(arguments, ", ");
var calls = arraySlice(arguments);
var i = calls.length;
while (i) {
if (!calls[--i].called) {
splice(calls, i, 1);
callOrder: function assertCallOrder() {
verifyIsStub.apply(null, arguments);
var expected = "";
var actual = "";
if (!calledInOrder(arguments)) {
try {
expected = join(arguments, ", ");
var calls = arraySlice(arguments);
var i = calls.length;
while (i) {
if (!calls[--i].called) {
splice(calls, i, 1);
}
}
actual = join(orderByFirstCall(calls), ", ");
} catch (e) {
// If this fails, we'll just fall back to the blank string
}
actual = join(orderByFirstCall(calls), ", ");
} catch (e) {
// If this fails, we'll just fall back to the blank string
failAssertion(this, "expected " + expected + " to be called in order but were called as " + actual);
} else {
assert.pass("callOrder");
}
},
failAssertion(this, "expected " + expected + " to be called in order but were called as " + actual);
} else {
assert.pass("callOrder");
}
},
callCount: function assertCallCount(method, count) {
verifyIsStub(method);
callCount: function assertCallCount(method, count) {
verifyIsStub(method);
if (method.callCount !== count) {
var msg = "expected %n to be called " + timesInWords(count) + " but was called %c%C";
failAssertion(this, method.printf(msg));
} else {
assert.pass("callCount");
}
},
if (method.callCount !== count) {
var msg = "expected %n to be called " + timesInWords(count) + " but was called %c%C";
failAssertion(this, method.printf(msg));
} else {
assert.pass("callCount");
}
},
expose: function expose(target, options) {
if (!target) {
throw new TypeError("target is null or undefined");
}
expose: function expose(target, options) {
if (!target) {
throw new TypeError("target is null or undefined");
}
var o = options || {};
var prefix = (typeof o.prefix === "undefined" && "assert") || o.prefix;
var includeFail = typeof o.includeFail === "undefined" || Boolean(o.includeFail);
var instance = this;
var o = options || {};
var prefix = (typeof o.prefix === "undefined" && "assert") || o.prefix;
var includeFail = typeof o.includeFail === "undefined" || Boolean(o.includeFail);
var instance = this;
forEach(Object.keys(instance), function(method) {
if (method !== "expose" && (includeFail || !/^(fail)/.test(method))) {
target[exposedName(prefix, method)] = instance[method];
}
});
forEach(Object.keys(instance), function(method) {
if (method !== "expose" && (includeFail || !/^(fail)/.test(method))) {
target[exposedName(prefix, method)] = instance[method];
}
});
return target;
},
return target;
},
match: function match(actual, expectation) {
var matcher = createMatcher(expectation);
if (matcher.test(actual)) {
assert.pass("match");
} else {
var formatted = [
"expected value to match",
" expected = " + format(expectation),
" actual = " + format(actual)
];
match: function match(actual, expectation) {
var matcher = createMatcher(expectation);
if (matcher.test(actual)) {
assert.pass("match");
} else {
var formatted = [
"expected value to match",
" expected = " + format(expectation),
" actual = " + format(actual)
];
failAssertion(this, join(formatted, "\n"));
failAssertion(this, join(formatted, "\n"));
}
}
}
};
};
mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
mirrorPropAsAssertion(
"notCalled",
function(spy) {
return !spy.called;
},
"expected %n to not have been called but was called %c%C"
);
mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %D");
mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %D");
mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %D");
mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %D");
mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %D");
mirrorPropAsAssertion("calledOnceWithExactly", "expected %n to be called once and with exact arguments %D");
mirrorPropAsAssertion("calledOnceWithMatch", "expected %n to be called once and with match %D");
mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %D");
mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
mirrorPropAsAssertion("threw", "%n did not throw exception%C");
mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
mirrorPropAsAssertion(
"notCalled",
function(spy) {
return !spy.called;
},
"expected %n to not have been called but was called %c%C"
);
mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %D");
mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %D");
mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %D");
mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %D");
mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %D");
mirrorPropAsAssertion("calledOnceWithExactly", "expected %n to be called once and with exact arguments %D");
mirrorPropAsAssertion("calledOnceWithMatch", "expected %n to be called once and with match %D");
mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %D");
mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
mirrorPropAsAssertion("threw", "%n did not throw exception%C");
mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
module.exports = assert;
return assert;
}
module.exports = createAssertObject();
module.exports.createAssertObject = createAssertObject;

@@ -40,68 +40,79 @@ "use strict";

function fake(f) {
if (arguments.length > 0 && typeof f !== "function") {
throw new TypeError("Expected f argument to be a Function");
}
function fakeClass() {
var promiseLib = Promise;
return wrapFunc(f);
}
function fake(f) {
if (arguments.length > 0 && typeof f !== "function") {
throw new TypeError("Expected f argument to be a Function");
}
fake.returns = function returns(value) {
function f() {
return value;
return wrapFunc(f);
}
return wrapFunc(f);
};
fake.returns = function returns(value) {
function f() {
return value;
}
fake.throws = function throws(value) {
function f() {
throw getError(value);
}
return wrapFunc(f);
};
return wrapFunc(f);
};
fake.throws = function throws(value) {
function f() {
throw getError(value);
}
fake.resolves = function resolves(value) {
function f() {
return Promise.resolve(value);
}
return wrapFunc(f);
};
return wrapFunc(f);
};
fake.resolves = function resolves(value) {
function f() {
return promiseLib.resolve(value);
}
fake.rejects = function rejects(value) {
function f() {
return Promise.reject(getError(value));
}
return wrapFunc(f);
};
return wrapFunc(f);
};
fake.rejects = function rejects(value) {
function f() {
return promiseLib.reject(getError(value));
}
function yieldInternal(async, values) {
function f() {
var callback = arguments[arguments.length - 1];
if (typeof callback !== "function") {
throw new TypeError("Expected last argument to be a function");
}
if (async) {
nextTick(function() {
return wrapFunc(f);
};
fake.usingPromise = function usingPromise(promiseLibrary) {
promiseLib = promiseLibrary;
return fake;
};
function yieldInternal(async, values) {
function f() {
var callback = arguments[arguments.length - 1];
if (typeof callback !== "function") {
throw new TypeError("Expected last argument to be a function");
}
if (async) {
nextTick(function() {
callback.apply(null, values);
});
} else {
callback.apply(null, values);
});
} else {
callback.apply(null, values);
}
}
return wrapFunc(f);
}
return wrapFunc(f);
}
fake.yields = function yields() {
return yieldInternal(false, slice(arguments));
};
fake.yields = function yields() {
return yieldInternal(false, slice(arguments));
};
fake.yieldsAsync = function yieldsAsync() {
return yieldInternal(true, slice(arguments));
};
fake.yieldsAsync = function yieldsAsync() {
return yieldInternal(true, slice(arguments));
};
return fake;
}
module.exports = fake;
module.exports = fakeClass();

@@ -20,3 +20,3 @@ "use strict";

var filter = arrayProto.filter;
var forEach = arrayProto.filter;
var forEach = arrayProto.forEach;
var push = arrayProto.push;

@@ -41,2 +41,4 @@ var reverse = arrayProto.reverse;

sandbox.assert = sinonAssert.createAssertObject();
sandbox.serverPrototype = fakeServer;

@@ -415,5 +417,4 @@

Sandbox.prototype.assert = sinonAssert;
Sandbox.prototype.match = match;
module.exports = Sandbox;

@@ -13,2 +13,3 @@ "use strict";

var push = arrayProto.push;
var slice = arrayProto.slice;

@@ -42,2 +43,9 @@ function colorSinonMatchText(matcher, calledArg, calledArgMessage) {

function quoteStringValue(value) {
if (typeof value === "string") {
return JSON.stringify(value);
}
return value;
}
module.exports = {

@@ -49,3 +57,3 @@ c: function(spyInstance) {

n: function(spyInstance) {
// eslint-disable-next-line local-rules/no-prototype-methods
// eslint-disable-next-line @sinonjs/no-prototype-methods/no-prototype-methods
return spyInstance.toString();

@@ -63,9 +71,20 @@ },

var calledArgs = spyInstance.getCall(i).args;
for (var j = 0; j < calledArgs.length || j < args.length; ++j) {
var expectedArgs = slice(args);
for (var j = 0; j < calledArgs.length || j < expectedArgs.length; ++j) {
if (calledArgs[j]) {
calledArgs[j] = quoteStringValue(calledArgs[j]);
}
if (expectedArgs[j]) {
expectedArgs[j] = quoteStringValue(expectedArgs[j]);
}
message += "\n";
var calledArgMessage = j < calledArgs.length ? sinonFormat(calledArgs[j]) : "";
if (match.isMatcher(args[j])) {
message += colorSinonMatchText(args[j], calledArgs[j], calledArgMessage);
if (match.isMatcher(expectedArgs[j])) {
message += colorSinonMatchText(expectedArgs[j], calledArgs[j], calledArgMessage);
} else {
var expectedArgMessage = j < args.length ? sinonFormat(args[j]) : "";
var expectedArgMessage = j < expectedArgs.length ? sinonFormat(expectedArgs[j]) : "";
var diff = jsDiff.diffJson(calledArgMessage, expectedArgMessage);

@@ -84,3 +103,3 @@ message += colorDiffText(diff);

for (var i = 0, l = spyInstance.callCount; i < l; ++i) {
// eslint-disable-next-line local-rules/no-prototype-methods
// eslint-disable-next-line @sinonjs/no-prototype-methods/no-prototype-methods
var stringifiedCall = " " + spyInstance.getCall(i).toString();

@@ -87,0 +106,0 @@ if (/\n/.test(calls[i - 1])) {

@@ -77,3 +77,3 @@ "use strict";

/* eslint-disable local-rules/no-prototype-methods */
/* eslint-disable @sinonjs/no-prototype-methods/no-prototype-methods */
var delegateToCalls = proxyCallUtil.delegateToCalls;

@@ -109,3 +109,2 @@ delegateToCalls(spyApi, "callArg", false, "callArgWith", true, function() {

});
/* eslint-enable local-rules/no-prototype-methods */

@@ -112,0 +111,0 @@ function createSpy(func) {

@@ -76,4 +76,4 @@ "use strict";

/** Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will
* override properties in previous sources.
/** Public: Extend target in place with all (own) properties, except 'name' when [[writable]] is false,
* from sources in-order. Thus, last source will override properties in previous sources.
*

@@ -89,3 +89,15 @@ * @arg {Object} target - The Object to extend

return extendCommon(target, sources, function copyValue(dest, source, prop) {
dest[prop] = source[prop];
var destOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(dest, prop);
var sourceOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(source, prop);
if (prop === "name" && !destOwnPropertyDescriptor.writable) {
return;
}
Object.defineProperty(dest, prop, {
configurable: sourceOwnPropertyDescriptor.configurable,
enumerable: sourceOwnPropertyDescriptor.enumerable,
writable: sourceOwnPropertyDescriptor.writable,
value: sourceOwnPropertyDescriptor.value
});
});

@@ -92,0 +104,0 @@ };

@@ -64,3 +64,3 @@ "use strict";

// Firefox has a problem when using hasOwn.call on objects from other frames.
/* eslint-disable-next-line local-rules/no-prototype-methods */
/* eslint-disable-next-line @sinonjs/no-prototype-methods/no-prototype-methods */
var owned = object.hasOwnProperty ? object.hasOwnProperty(property) : hasOwnProperty(object, property);

@@ -67,0 +67,0 @@

{
"name": "sinon",
"description": "JavaScript test spies, stubs and mocks.",
"version": "9.2.0",
"version": "9.2.1",
"homepage": "https://sinonjs.org/",

@@ -70,2 +70,3 @@ "author": "Christian Johansen",

"@babel/core": "^7.8.3",
"@sinonjs/eslint-plugin-no-prototype-methods": "^0.1.0",
"@sinonjs/referee": "^6.1.0",

@@ -80,3 +81,2 @@ "babel-plugin-istanbul": "^6.0.0",

"eslint-plugin-ie11": "^1.0.0",
"eslint-plugin-local-rules": "^0.1.0",
"eslint-plugin-mocha": "^6.1.0",

@@ -83,0 +83,0 @@ "eslint-plugin-prettier": "^3.1.0",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc