From 287962caccec8aceacac32e8c91c42db87b21258 Mon Sep 17 00:00:00 2001 From: Ben Kucera <14625260+Bkucera@users.noreply.github.com> Date: Tue, 12 Feb 2019 19:49:19 -0500 Subject: [PATCH 1/4] add diff support for 'calledWith*' matchers --- lib/sinon-chai.js | 24 +++++++++++- test/messages.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/lib/sinon-chai.js b/lib/sinon-chai.js index 194dc3a..534e73f 100644 --- a/lib/sinon-chai.js +++ b/lib/sinon-chai.js @@ -102,10 +102,30 @@ var sinonMethodName = shouldBeAlways ? alwaysSinonMethod : sinonName; var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments)); + var passed = this._obj[sinonMethodName].apply(this._obj, arguments); + var actual; + var expected; + if (!passed) { + if (sinonMethodName.indexOf("calledWith") === 0) { + var lastCall = this._obj.lastCall || this._obj; + actual = lastCall.args && lastCall.args; + if (this._obj.callCount === 0) { + actual = undefined; + } + + expected = slice.call(arguments); + } + } + + var enableDiff = !passed; + this.assert( - this._obj[sinonMethodName].apply(this._obj, arguments), + passed, messages.affirmative, - messages.negative + messages.negative, + expected, + actual, + enableDiff ); }; } diff --git a/test/messages.js b/test/messages.js index a233462..5f23b57 100644 --- a/test/messages.js +++ b/test/messages.js @@ -472,6 +472,105 @@ describe("Messages", function () { }); }); + describe("diff support", function () { + it("should add actual/expected and set enableDiff to true", function () { + var spy = sinon.spy(); + spy("foo1"); + + var err; + + try { + expect(spy).to.have.been.calledWith("bar"); + } catch (e) { + err = e; + } + expect(err).ok; + expect(err).to.ownProperty("showDiff", true); + expect(err).to.ownProperty("actual").deep.eq(["foo1"]); + expect(err).to.ownProperty("expected").deep.eq(["bar"]); + }); + + it("should use lastCall args if exists", function () { + var spy = sinon.spy(); + spy("foo1"); + spy("foo2"); + + var err; + + try { + expect(spy).to.have.been.calledWith("bar"); + } catch (e) { + err = e; + } + expect(err).ok; + expect(err).to.ownProperty("showDiff", true); + expect(err).to.ownProperty("actual").deep.eq(["foo2"]); + expect(err).to.ownProperty("expected").deep.eq(["bar"]); + }); + + it("should use args if no lastCall", function () { + var spy = sinon.spy(); + spy("foo1"); + spy("foo2"); + + var err; + + try { + expect(spy.firstCall).to.have.been.calledWith("bar"); + } catch (e) { + err = e; + } + expect(err).ok; + expect(err).to.ownProperty("showDiff", true); + expect(err).to.ownProperty("actual").deep.eq(["foo1"]); + expect(err).to.ownProperty("expected").deep.eq(["bar"]); + }); + + it("should use undefined if no call", function () { + var spy = sinon.spy(); + + var err; + + try { + expect(spy).to.have.been.calledWith("bar"); + } catch (e) { + err = e; + } + expect(err).ok; + expect(err).to.ownProperty("showDiff", true); + expect(err).to.ownProperty("actual").deep.eq(undefined); + expect(err).to.ownProperty("expected").deep.eq(["bar"]); + }); + + it("should not add actual/expected and set enableDiff to true if passes", function () { + var spy = sinon.spy(); + spy("foo", "bar", "baz"); + + var err; + + try { + spy.should.calledWithExactly("foo", "bar", "baz"); + } catch (e) { + err = e; + } + expect(err).to.be.undefined; + }); + + it("should not add actual/expected and set enableDiff if not 'calledWith' matcher", function () { + var spy = sinon.spy(); + spy("foo", "bar", "baz"); + + var err; + + try { + expect(spy).to.have.been.calledTwice(); + } catch (e) { + err = e; + } + expect(err).ownProperty("showDiff").eq(false); + }); + }); + describe("when used on a non-spy/non-call", function () { function notSpy() { // Contents don't matter From f10b44b26c3710cbf68f8a0e78c0be37aca95da4 Mon Sep 17 00:00:00 2001 From: Ben Kucera <14625260+Bkucera@users.noreply.github.com> Date: Fri, 9 Aug 2019 11:36:03 -0400 Subject: [PATCH 2/4] support calledOnceWith* matchers, upgrade devDeps --- lib/sinon-chai.js | 7 +++++-- package.json | 4 ++-- test/messages.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/sinon-chai.js b/lib/sinon-chai.js index 534e73f..d708d1e 100644 --- a/lib/sinon-chai.js +++ b/lib/sinon-chai.js @@ -93,10 +93,13 @@ }); } + function isDiffSupportedMethod(sinonMethodName) { + return (sinonMethodName.indexOf("calledWith") === 0) || (sinonMethodName.indexOf("calledOnceWith") === 0); + } + function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) { 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; @@ -106,7 +109,7 @@ var actual; var expected; if (!passed) { - if (sinonMethodName.indexOf("calledWith") === 0) { + if (isDiffSupportedMethod(sinonMethodName)) { var lastCall = this._obj.lastCall || this._obj; actual = lastCall.args && lastCall.args; if (this._obj.callCount === 0) { diff --git a/package.json b/package.json index e69b05f..60de163 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,9 @@ }, "devDependencies": { "chai": "^4.1.2", - "eslint": "^3.19.0", + "eslint": "^6.1.0", "istanbul": "~0.4.5", - "mocha": "^3.5.3", + "mocha": "^6.2.0", "opener": "^1.4.3", "sinon": "^7.0.0" } diff --git a/test/messages.js b/test/messages.js index 5f23b57..906bf23 100644 --- a/test/messages.js +++ b/test/messages.js @@ -490,6 +490,40 @@ describe("Messages", function () { expect(err).to.ownProperty("expected").deep.eq(["bar"]); }); + it("should add diff when calledOnceWith matcher", function () { + var spy = sinon.spy(); + spy("foo1"); + + var err; + + try { + expect(spy).to.have.been.calledOnceWith("bar"); + } catch (e) { + err = e; + } + expect(err).ok; + expect(err).to.ownProperty("showDiff", true); + expect(err).to.ownProperty("actual").deep.eq(["foo1"]); + expect(err).to.ownProperty("expected").deep.eq(["bar"]); + }); + + it("should add diff when calledOnceWithExactly matcher", function () { + var spy = sinon.spy(); + spy("foo1"); + + var err; + + try { + expect(spy).to.have.been.calledOnceWithExactly("bar"); + } catch (e) { + err = e; + } + expect(err).ok; + expect(err).to.ownProperty("showDiff", true); + expect(err).to.ownProperty("actual").deep.eq(["foo1"]); + expect(err).to.ownProperty("expected").deep.eq(["bar"]); + }); + it("should use lastCall args if exists", function () { var spy = sinon.spy(); spy("foo1"); From 951b2839b25d1ad0f8941c7fc15cc39ea5ab3557 Mon Sep 17 00:00:00 2001 From: Ben Kucera <14625260+Bkucera@users.noreply.github.com> Date: Fri, 9 Aug 2019 11:57:10 -0400 Subject: [PATCH 3/4] revert devDeps upgrade --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 60de163..e69b05f 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,9 @@ }, "devDependencies": { "chai": "^4.1.2", - "eslint": "^6.1.0", + "eslint": "^3.19.0", "istanbul": "~0.4.5", - "mocha": "^6.2.0", + "mocha": "^3.5.3", "opener": "^1.4.3", "sinon": "^7.0.0" } From 43d0ea3df3dbd4ddf4a7c09adc95ae4a869d7af7 Mon Sep 17 00:00:00 2001 From: Ben Kucera <14625260+Bkucera@users.noreply.github.com> Date: Fri, 9 Aug 2019 12:01:41 -0400 Subject: [PATCH 4/4] cleanup --- lib/sinon-chai.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sinon-chai.js b/lib/sinon-chai.js index d708d1e..ac09536 100644 --- a/lib/sinon-chai.js +++ b/lib/sinon-chai.js @@ -108,8 +108,10 @@ var passed = this._obj[sinonMethodName].apply(this._obj, arguments); var actual; var expected; + var enableDiff; if (!passed) { if (isDiffSupportedMethod(sinonMethodName)) { + enableDiff = true; var lastCall = this._obj.lastCall || this._obj; actual = lastCall.args && lastCall.args; if (this._obj.callCount === 0) { @@ -120,8 +122,6 @@ } } - var enableDiff = !passed; - this.assert( passed, messages.affirmative,