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

Bring calledWith messages more in line with Sinon's output #152

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
52 changes: 32 additions & 20 deletions lib/sinon-chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,21 @@
}
}

function getMessages(spy, action, nonNegatedSuffix, always, args) {
function getMessages(spy, action, suffixes, always, args) {
var verbPhrase = always ? "always have " : "have ";
nonNegatedSuffix = nonNegatedSuffix || "";

// Suffixes is [nonNegatedSuffix, negatedSuffix], or just
// nonNegatedSuffix if there is no negatedSuffix
var negatedSuffix;
var nonNegatedSuffix;
if (Array.isArray(suffixes)) {
nonNegatedSuffix = suffixes[0] || "";
negatedSuffix = suffixes[1] || "";
} else {
nonNegatedSuffix = suffixes || "";
negatedSuffix = "";
}

if (isSpy(spy.proxy)) {
spy = spy.proxy;
}
Expand All @@ -70,38 +82,38 @@
return printfArray(["expected %n to " + verbPhrase + action + nonNegatedSuffix].concat(args));
},
negative: function () {
return printfArray(["expected %n to not " + verbPhrase + action].concat(args));
return printfArray(["expected %n to not " + verbPhrase + action + negatedSuffix].concat(args));
}
};
}

function sinonProperty(name, action, nonNegatedSuffix) {
function sinonProperty(name, action, suffixes) {
utils.addProperty(chai.Assertion.prototype, name, function () {
assertCanWorkWith(this);

var messages = getMessages(this._obj, action, nonNegatedSuffix, false);
var messages = getMessages(this._obj, action, suffixes, false);
this.assert(this._obj[name], messages.affirmative, messages.negative);
});
}

function sinonPropertyAsBooleanMethod(name, action, nonNegatedSuffix) {
function sinonPropertyAsBooleanMethod(name, action, suffixes) {
utils.addMethod(chai.Assertion.prototype, name, function (arg) {
assertCanWorkWith(this);

var messages = getMessages(this._obj, action, nonNegatedSuffix, false, [timesInWords(arg)]);
var messages = getMessages(this._obj, action, suffixes, false, [timesInWords(arg)]);
this.assert(this._obj[name] === arg, messages.affirmative, messages.negative);
});
}

function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) {
function createSinonMethodHandler(sinonName, action, suffixes) {
return function () {
assertCanWorkWith(this);

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;

var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments));
var messages = getMessages(this._obj, action, suffixes, shouldBeAlways, slice.call(arguments));
this.assert(
this._obj[sinonMethodName].apply(this._obj, arguments),
messages.affirmative,
Expand All @@ -110,18 +122,18 @@
};
}

function sinonMethodAsProperty(name, action, nonNegatedSuffix) {
var handler = createSinonMethodHandler(name, action, nonNegatedSuffix);
function sinonMethodAsProperty(name, action, suffixes) {
var handler = createSinonMethodHandler(name, action, suffixes);
utils.addProperty(chai.Assertion.prototype, name, handler);
}

function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffix) {
var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix);
function exceptionalSinonMethod(chaiName, sinonName, action, suffixes) {
var handler = createSinonMethodHandler(sinonName, action, suffixes);
utils.addMethod(chai.Assertion.prototype, chaiName, handler);
}

function sinonMethod(name, action, nonNegatedSuffix) {
exceptionalSinonMethod(name, name, action, nonNegatedSuffix);
function sinonMethod(name, action, suffixes) {
exceptionalSinonMethod(name, name, action, suffixes);
}

utils.addProperty(chai.Assertion.prototype, "always", function () {
Expand All @@ -139,11 +151,11 @@
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("calledWith", "been called with arguments ", ["%D", "%*%C"]);
sinonMethod("calledOnceWith", "been called exactly once with arguments ", ["%D", "%*%C"]);
sinonMethod("calledWithExactly", "been called with exact arguments ", ["%D", "%*%C"]);
sinonMethod("calledOnceWithExactly", "been called exactly once with exact arguments ", ["%D", "%*%C"]);
sinonMethod("calledWithMatch", "been called with arguments matching ", ["%D", "%*%C"]);
sinonMethod("returned", "returned %1");
exceptionalSinonMethod("thrown", "threw", "thrown %1");
}));
116 changes: 103 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"opener": "^1.5.1",
"sinon": "^9.0.0"
"sinon": "^9.0.0",
"strip-ansi": "^6.0.0"
}
}
Loading