sinon-chai
Advanced tools
Comparing version 4.0.0-0 to 4.0.0
@@ -1,131 +0,195 @@ | ||
/* eslint-disable no-invalid-this */ | ||
export default function (chai, utils) { | ||
var slice = Array.prototype.slice; | ||
const slice = Array.prototype.slice; | ||
function isSpy(putativeSpy) { | ||
return typeof putativeSpy === "function" && | ||
typeof putativeSpy.getCall === "function" && | ||
typeof putativeSpy.calledWithExactly === "function"; | ||
function isSpy(putativeSpy) { | ||
return ( | ||
typeof putativeSpy === 'function' && | ||
typeof putativeSpy.getCall === 'function' && | ||
typeof putativeSpy.calledWithExactly === 'function' | ||
); | ||
} | ||
function timesInWords(count) { | ||
switch (count) { | ||
case 1: { | ||
return 'once'; | ||
} | ||
case 2: { | ||
return 'twice'; | ||
} | ||
case 3: { | ||
return 'thrice'; | ||
} | ||
default: { | ||
return (count || 0) + ' times'; | ||
} | ||
} | ||
} | ||
function timesInWords(count) { | ||
switch (count) { | ||
case 1: { | ||
return "once"; | ||
} | ||
case 2: { | ||
return "twice"; | ||
} | ||
case 3: { | ||
return "thrice"; | ||
} | ||
default: { | ||
return (count || 0) + " times"; | ||
} | ||
} | ||
function isCall(putativeCall) { | ||
return putativeCall && isSpy(putativeCall.proxy); | ||
} | ||
function assertCanWorkWith(assertion) { | ||
if (!isSpy(assertion._obj) && !isCall(assertion._obj)) { | ||
throw new TypeError( | ||
utils.inspect(assertion._obj) + ' is not a spy or a call to a spy!' | ||
); | ||
} | ||
} | ||
function isCall(putativeCall) { | ||
return putativeCall && isSpy(putativeCall.proxy); | ||
function getMessages(spy, action, nonNegatedSuffix, always, args) { | ||
var verbPhrase = always ? 'always have ' : 'have '; | ||
nonNegatedSuffix = nonNegatedSuffix || ''; | ||
if (isSpy(spy.proxy)) { | ||
spy = spy.proxy; | ||
} | ||
function assertCanWorkWith(assertion) { | ||
if (!isSpy(assertion._obj) && !isCall(assertion._obj)) { | ||
throw new TypeError(utils.inspect(assertion._obj) + " is not a spy or a call to a spy!"); | ||
} | ||
function printfArray(array) { | ||
return spy.printf.apply(spy, array); | ||
} | ||
function getMessages(spy, action, nonNegatedSuffix, always, args) { | ||
var verbPhrase = always ? "always have " : "have "; | ||
nonNegatedSuffix = nonNegatedSuffix || ""; | ||
if (isSpy(spy.proxy)) { | ||
spy = spy.proxy; | ||
} | ||
return { | ||
affirmative: function () { | ||
return printfArray( | ||
['expected %n to ' + verbPhrase + action + nonNegatedSuffix].concat( | ||
args | ||
) | ||
); | ||
}, | ||
negative: function () { | ||
return printfArray( | ||
['expected %n to not ' + verbPhrase + action].concat(args) | ||
); | ||
} | ||
}; | ||
} | ||
function printfArray(array) { | ||
return spy.printf.apply(spy, array); | ||
} | ||
function sinonProperty(name, action, nonNegatedSuffix) { | ||
utils.addProperty(chai.Assertion.prototype, name, function () { | ||
assertCanWorkWith(this); | ||
return { | ||
affirmative: function () { | ||
return printfArray(["expected %n to " + verbPhrase + action + nonNegatedSuffix].concat(args)); | ||
}, | ||
negative: function () { | ||
return printfArray(["expected %n to not " + verbPhrase + action].concat(args)); | ||
} | ||
}; | ||
} | ||
var messages = getMessages(this._obj, action, nonNegatedSuffix, false); | ||
this.assert(this._obj[name], messages.affirmative, messages.negative); | ||
}); | ||
} | ||
function sinonProperty(name, action, nonNegatedSuffix) { | ||
utils.addProperty(chai.Assertion.prototype, name, function () { | ||
assertCanWorkWith(this); | ||
function sinonPropertyAsBooleanMethod(name, action, nonNegatedSuffix) { | ||
utils.addMethod(chai.Assertion.prototype, name, function (arg) { | ||
assertCanWorkWith(this); | ||
var messages = getMessages(this._obj, action, nonNegatedSuffix, false); | ||
this.assert(this._obj[name], messages.affirmative, messages.negative); | ||
}); | ||
} | ||
var messages = getMessages(this._obj, action, nonNegatedSuffix, false, [ | ||
timesInWords(arg) | ||
]); | ||
this.assert( | ||
this._obj[name] === arg, | ||
messages.affirmative, | ||
messages.negative | ||
); | ||
}); | ||
} | ||
function sinonPropertyAsBooleanMethod(name, action, nonNegatedSuffix) { | ||
utils.addMethod(chai.Assertion.prototype, name, function (arg) { | ||
assertCanWorkWith(this); | ||
function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) { | ||
return function () { | ||
assertCanWorkWith(this); | ||
var messages = getMessages(this._obj, action, nonNegatedSuffix, false, [timesInWords(arg)]); | ||
this.assert(this._obj[name] === arg, messages.affirmative, messages.negative); | ||
}); | ||
} | ||
var alwaysSinonMethod = | ||
'always' + sinonName[0].toUpperCase() + sinonName.substring(1); | ||
var shouldBeAlways = | ||
utils.flag(this, 'always') && | ||
typeof this._obj[alwaysSinonMethod] === 'function'; | ||
var sinonMethodName = shouldBeAlways ? alwaysSinonMethod : sinonName; | ||
function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) { | ||
return function () { | ||
assertCanWorkWith(this); | ||
var messages = getMessages( | ||
this._obj, | ||
action, | ||
nonNegatedSuffix, | ||
shouldBeAlways, | ||
slice.call(arguments) | ||
); | ||
this.assert( | ||
this._obj[sinonMethodName].apply(this._obj, arguments), | ||
messages.affirmative, | ||
messages.negative | ||
); | ||
}; | ||
} | ||
var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1); | ||
var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[alwaysSinonMethod] === "function"; | ||
var sinonMethodName = shouldBeAlways ? alwaysSinonMethod : sinonName; | ||
function sinonMethodAsProperty(name, action, nonNegatedSuffix) { | ||
var handler = createSinonMethodHandler(name, action, nonNegatedSuffix); | ||
utils.addProperty(chai.Assertion.prototype, name, handler); | ||
} | ||
var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments)); | ||
this.assert( | ||
this._obj[sinonMethodName].apply(this._obj, arguments), | ||
messages.affirmative, | ||
messages.negative | ||
); | ||
}; | ||
} | ||
function exceptionalSinonMethod( | ||
chaiName, | ||
sinonName, | ||
action, | ||
nonNegatedSuffix | ||
) { | ||
var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix); | ||
utils.addMethod(chai.Assertion.prototype, chaiName, handler); | ||
} | ||
function sinonMethodAsProperty(name, action, nonNegatedSuffix) { | ||
var handler = createSinonMethodHandler(name, action, nonNegatedSuffix); | ||
utils.addProperty(chai.Assertion.prototype, name, handler); | ||
} | ||
function sinonMethod(name, action, nonNegatedSuffix) { | ||
exceptionalSinonMethod(name, name, action, nonNegatedSuffix); | ||
} | ||
function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffix) { | ||
var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix); | ||
utils.addMethod(chai.Assertion.prototype, chaiName, handler); | ||
} | ||
utils.addProperty(chai.Assertion.prototype, 'always', function () { | ||
utils.flag(this, 'always', true); | ||
}); | ||
function sinonMethod(name, action, nonNegatedSuffix) { | ||
exceptionalSinonMethod(name, name, action, nonNegatedSuffix); | ||
} | ||
utils.addProperty(chai.Assertion.prototype, "always", function () { | ||
utils.flag(this, "always", true); | ||
}); | ||
sinonProperty("called", "been called", " at least once, but it was never called"); | ||
sinonPropertyAsBooleanMethod("callCount", "been called exactly %1", ", but it was called %c%C"); | ||
sinonProperty("calledOnce", "been called exactly once", ", but it was called %c%C"); | ||
sinonProperty("calledTwice", "been called exactly twice", ", but it was called %c%C"); | ||
sinonProperty("calledThrice", "been called exactly thrice", ", but it was called %c%C"); | ||
sinonMethodAsProperty("calledWithNew", "been called with new"); | ||
sinonMethod("calledBefore", "been called before %1"); | ||
sinonMethod("calledAfter", "been called after %1"); | ||
sinonMethod("calledImmediatelyBefore", "been called immediately before %1"); | ||
sinonMethod("calledImmediatelyAfter", "been called immediately after %1"); | ||
sinonMethod("calledOn", "been called with %1 as this", ", but it was called with %t instead"); | ||
sinonMethod("calledWith", "been called with arguments %*", "%D"); | ||
sinonMethod("calledOnceWith", "been called exactly once with arguments %*", "%D"); | ||
sinonMethod("calledWithExactly", "been called with exact arguments %*", "%D"); | ||
sinonMethod("calledOnceWithExactly", "been called exactly once with exact arguments %*", "%D"); | ||
sinonMethod("calledWithMatch", "been called with arguments matching %*", "%D"); | ||
sinonMethod("returned", "returned %1"); | ||
exceptionalSinonMethod("thrown", "threw", "thrown %1"); | ||
sinonProperty( | ||
'called', | ||
'been called', | ||
' at least once, but it was never called' | ||
); | ||
sinonPropertyAsBooleanMethod( | ||
'callCount', | ||
'been called exactly %1', | ||
', but it was called %c%C' | ||
); | ||
sinonProperty( | ||
'calledOnce', | ||
'been called exactly once', | ||
', but it was called %c%C' | ||
); | ||
sinonProperty( | ||
'calledTwice', | ||
'been called exactly twice', | ||
', but it was called %c%C' | ||
); | ||
sinonProperty( | ||
'calledThrice', | ||
'been called exactly thrice', | ||
', but it was called %c%C' | ||
); | ||
sinonMethodAsProperty('calledWithNew', 'been called with new'); | ||
sinonMethod('calledBefore', 'been called before %1'); | ||
sinonMethod('calledAfter', 'been called after %1'); | ||
sinonMethod('calledImmediatelyBefore', 'been called immediately before %1'); | ||
sinonMethod('calledImmediatelyAfter', 'been called immediately after %1'); | ||
sinonMethod( | ||
'calledOn', | ||
'been called with %1 as this', | ||
', but it was called with %t instead' | ||
); | ||
sinonMethod('calledWith', 'been called with arguments %*', '%D'); | ||
sinonMethod( | ||
'calledOnceWith', | ||
'been called exactly once with arguments %*', | ||
'%D' | ||
); | ||
sinonMethod('calledWithExactly', 'been called with exact arguments %*', '%D'); | ||
sinonMethod( | ||
'calledOnceWithExactly', | ||
'been called exactly once with exact arguments %*', | ||
'%D' | ||
); | ||
sinonMethod( | ||
'calledWithMatch', | ||
'been called with arguments matching %*', | ||
'%D' | ||
); | ||
sinonMethod('returned', 'returned %1'); | ||
exceptionalSinonMethod('thrown', 'threw', 'thrown %1'); | ||
} |
{ | ||
"name": "sinon-chai", | ||
"type": "module", | ||
"description": "Extends Chai with assertions for the Sinon.JS mocking framework.", | ||
@@ -16,3 +17,3 @@ "keywords": [ | ||
], | ||
"version": "4.0.0-0", | ||
"version": "4.0.0", | ||
"author": "Domenic Denicola <d@domenic.me> (https://domenic.me/)", | ||
@@ -23,4 +24,10 @@ "contributors": [ | ||
"license": "(BSD-2-Clause OR WTFPL)", | ||
"repository": "domenic/sinon-chai", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/chaijs/sinon-chai.git" | ||
}, | ||
"main": "./lib/sinon-chai.js", | ||
"exports": { | ||
".": "./lib/sinon-chai.js" | ||
}, | ||
"files": [ | ||
@@ -30,6 +37,6 @@ "lib" | ||
"scripts": { | ||
"test": "mocha", | ||
"test-travis": "npm install && npm install chai@$CHAI_VERSION && npm install sinon@$SINON_VERSION && npm test", | ||
"test": "c8 mocha", | ||
"test-ci": "npm install && npm install chai@$CHAI_VERSION && npm install sinon@$SINON_VERSION && npm test", | ||
"lint": "eslint .", | ||
"cover": "nyc --reporter=html mocha && opener ./coverage/sinon-chai.js.html" | ||
"format": "prettier --write lib" | ||
}, | ||
@@ -41,10 +48,10 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@eslint/js": "^9.7.0", | ||
"c8": "^10.1.2", | ||
"chai": "^5.0.0", | ||
"eslint": "^6.8.0", | ||
"mocha": "^10.0.0", | ||
"nyc": "^15.0.0", | ||
"opener": "^1.5.1", | ||
"eslint": "^8.57.0", | ||
"mocha": "^10.7.0", | ||
"prettier": "^3.3.3", | ||
"sinon": "^9.0.0" | ||
}, | ||
"type": "module" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
176
1
0
0
16016
7