Skip to content

Commit f1cac90

Browse files
kucebbrian-mann
authored andcommitted
feat: add diff support for 'calledWith*' matchers (#2)
1 parent fa90a35 commit f1cac90

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

lib/sinon-chai.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,30 @@
126126
var passed = utils.test(this, [expression]);
127127
var messages = getMessages(passed,
128128
this._obj, action, passedSuffix, failedSuffix, shouldBeAlways, slice.call(arguments));
129-
this.assert(expression, messages.affirmative, messages.negative);
129+
var actual;
130+
var expected;
131+
if (!passed) {
132+
if (sinonMethodName.indexOf("calledWith") === 0) {
133+
var lastCall = this._obj.lastCall || this._obj;
134+
actual = lastCall.args && lastCall.args;
135+
if (this._obj.callCount === 0) {
136+
actual = undefined;
137+
}
138+
139+
expected = slice.call(arguments);
140+
}
141+
}
142+
143+
var enableDiff = !passed;
144+
145+
this.assert(
146+
expression,
147+
messages.affirmative,
148+
messages.negative,
149+
expected,
150+
actual,
151+
enableDiff
152+
);
130153
};
131154
}
132155

test/messages.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,105 @@ describe("Messages", function () {
522522
});
523523
});
524524

525+
describe("diff support", function () {
526+
it("should add actual/expected and set enableDiff to true", function () {
527+
var spy = sinon.spy();
528+
spy("foo1");
529+
530+
var err;
531+
532+
try {
533+
expect(spy).to.have.been.calledWith("bar");
534+
} catch (e) {
535+
err = e;
536+
}
537+
expect(err).ok;
538+
expect(err).to.ownProperty("showDiff", true);
539+
expect(err).to.ownProperty("actual").deep.eq(["foo1"]);
540+
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
541+
});
542+
543+
it("should use lastCall args if exists", function () {
544+
var spy = sinon.spy();
545+
spy("foo1");
546+
spy("foo2");
547+
548+
var err;
549+
550+
try {
551+
expect(spy).to.have.been.calledWith("bar");
552+
} catch (e) {
553+
err = e;
554+
}
555+
expect(err).ok;
556+
expect(err).to.ownProperty("showDiff", true);
557+
expect(err).to.ownProperty("actual").deep.eq(["foo2"]);
558+
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
559+
});
560+
561+
it("should use args if no lastCall", function () {
562+
var spy = sinon.spy();
563+
spy("foo1");
564+
spy("foo2");
565+
566+
var err;
567+
568+
try {
569+
expect(spy.firstCall).to.have.been.calledWith("bar");
570+
} catch (e) {
571+
err = e;
572+
}
573+
expect(err).ok;
574+
expect(err).to.ownProperty("showDiff", true);
575+
expect(err).to.ownProperty("actual").deep.eq(["foo1"]);
576+
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
577+
});
578+
579+
it("should use undefined if no call", function () {
580+
var spy = sinon.spy();
581+
582+
var err;
583+
584+
try {
585+
expect(spy).to.have.been.calledWith("bar");
586+
} catch (e) {
587+
err = e;
588+
}
589+
expect(err).ok;
590+
expect(err).to.ownProperty("showDiff", true);
591+
expect(err).to.ownProperty("actual").deep.eq(undefined);
592+
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
593+
});
594+
595+
it("should not add actual/expected and set enableDiff to true if passes", function () {
596+
var spy = sinon.spy();
597+
spy("foo", "bar", "baz");
598+
599+
var err;
600+
601+
try {
602+
spy.should.calledWithExactly("foo", "bar", "baz");
603+
} catch (e) {
604+
err = e;
605+
}
606+
expect(err).to.be.undefined;
607+
});
608+
609+
it("should not add actual/expected and set enableDiff if not 'calledWith' matcher", function () {
610+
var spy = sinon.spy();
611+
spy("foo", "bar", "baz");
612+
613+
var err;
614+
615+
try {
616+
expect(spy).to.have.been.calledTwice();
617+
} catch (e) {
618+
err = e;
619+
}
620+
expect(err).ownProperty("showDiff").eq(false);
621+
});
622+
});
623+
525624
describe("when used on a non-spy/non-call", function () {
526625
function notSpy() {
527626
// Contents don't matter

0 commit comments

Comments
 (0)