diff --git a/lib/sinon-chai.js b/lib/sinon-chai.js index 194dc3a..4bbdf11 100644 --- a/lib/sinon-chai.js +++ b/lib/sinon-chai.js @@ -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; } @@ -70,30 +82,30 @@ 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); @@ -101,7 +113,7 @@ 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, @@ -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 () { @@ -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"); })); diff --git a/package-lock.json b/package-lock.json index cda1e57..f940282 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "sinon-chai", - "version": "3.4.0", + "version": "3.7.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -540,6 +540,12 @@ "wrap-ansi": "^5.1.0" }, "dependencies": { + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "dev": true + }, "emoji-regex": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", @@ -562,6 +568,15 @@ "is-fullwidth-code-point": "^2.0.0", "strip-ansi": "^5.1.0" } + }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "dev": true, + "requires": { + "ansi-regex": "^4.1.0" + } } } }, @@ -776,6 +791,23 @@ "table": "^5.2.3", "text-table": "^0.2.0", "v8-compile-cache": "^2.0.3" + }, + "dependencies": { + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "dev": true + }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "dev": true, + "requires": { + "ansi-regex": "^4.1.0" + } + } } }, "eslint-scope": { @@ -1213,6 +1245,23 @@ "string-width": "^4.1.0", "strip-ansi": "^5.1.0", "through": "^2.3.6" + }, + "dependencies": { + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "dev": true + }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "dev": true, + "requires": { + "ansi-regex": "^4.1.0" + } + } } }, "is-binary-path": { @@ -2492,20 +2541,12 @@ } }, "strip-ansi": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", - "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", "dev": true, "requires": { - "ansi-regex": "^4.1.0" - }, - "dependencies": { - "ansi-regex": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", - "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", - "dev": true - } + "ansi-regex": "^5.0.0" } }, "strip-bom": { @@ -2541,6 +2582,12 @@ "string-width": "^3.0.0" }, "dependencies": { + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "dev": true + }, "emoji-regex": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", @@ -2562,6 +2609,17 @@ "emoji-regex": "^7.0.1", "is-fullwidth-code-point": "^2.0.0", "strip-ansi": "^5.1.0" + }, + "dependencies": { + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "dev": true, + "requires": { + "ansi-regex": "^4.1.0" + } + } } } } @@ -2744,6 +2802,12 @@ "strip-ansi": "^5.0.0" }, "dependencies": { + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "dev": true + }, "emoji-regex": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", @@ -2766,6 +2830,15 @@ "is-fullwidth-code-point": "^2.0.0", "strip-ansi": "^5.1.0" } + }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "dev": true, + "requires": { + "ansi-regex": "^4.1.0" + } } } }, @@ -2820,6 +2893,12 @@ "yargs-parser": "^13.1.1" }, "dependencies": { + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "dev": true + }, "emoji-regex": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", @@ -2841,6 +2920,17 @@ "emoji-regex": "^7.0.1", "is-fullwidth-code-point": "^2.0.0", "strip-ansi": "^5.1.0" + }, + "dependencies": { + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "dev": true, + "requires": { + "ansi-regex": "^4.1.0" + } + } } } } diff --git a/package.json b/package.json index 51acb09..497db15 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/messages.js b/test/messages.js index a233462..293bf14 100644 --- a/test/messages.js +++ b/test/messages.js @@ -3,38 +3,59 @@ var sinon = require("sinon"); var expect = require("chai").expect; var swallow = require("./common").swallow; +var stripAnsi = require("strip-ansi"); + +function makeErrorMessageTransform(transform) { + return function (func) { + return function () { + try { + func(); + } catch (error) { + error.message = transform(error.message); + throw error; + } + }; + }; +} +var stripQuotes = makeErrorMessageTransform(function (message) { + return message.replace(/\\*['"]/g, ""); +}); +var stripColors = makeErrorMessageTransform(stripAnsi); +var stripQuotesAndColors = makeErrorMessageTransform(function (message) { + return stripAnsi(message).replace(/\\*['"]/g, ""); +}); describe("Messages", function () { describe("about call count", function () { it("should be correct for the base cases", function () { var spy = sinon.spy(); - expect(function () { + expect(stripQuotes(function () { spy.should.have.been.called; - }).to.throw("expected spy to have been called at least once, but it was never called"); - expect(function () { + })).to.throw("expected spy to have been called at least once, but it was never called"); + expect(stripQuotes(function () { spy.should.have.been.calledOnce; - }).to.throw("expected spy to have been called exactly once, but it was called 0 times"); - expect(function () { + })).to.throw("expected spy to have been called exactly once, but it was called 0 times"); + expect(stripQuotes(function () { spy.should.have.been.calledTwice; - }).to.throw("expected spy to have been called exactly twice, but it was called 0 times"); - expect(function () { + })).to.throw("expected spy to have been called exactly twice, but it was called 0 times"); + expect(stripQuotes(function () { spy.should.have.been.calledThrice; - }).to.throw("expected spy to have been called exactly thrice, but it was called 0 times"); + })).to.throw("expected spy to have been called exactly thrice, but it was called 0 times"); - expect(function () { + expect(stripQuotes(function () { spy.should.have.callCount(1); - }).to.throw("expected spy to have been called exactly once, but it was called 0 times"); - expect(function () { + })).to.throw("expected spy to have been called exactly once, but it was called 0 times"); + expect(stripQuotes(function () { spy.should.have.callCount(4); - }).to.throw("expected spy to have been called exactly 4 times, but it was called 0 times"); + })).to.throw("expected spy to have been called exactly 4 times, but it was called 0 times"); - expect(function () { + expect(stripQuotes(function () { spy.should.have.been.calledOnceWith(); - }).to.throw("expected spy to have been called exactly once with arguments"); - expect(function () { + })).to.throw("expected spy to have been called exactly once with arguments"); + expect(stripQuotes(function () { spy.should.have.been.calledOnceWithExactly(); - }).to.throw("expected spy to have been called exactly once with exact arguments"); + })).to.throw("expected spy to have been called exactly once with exact arguments"); }); it("should be correct for the negated cases", function () { @@ -58,29 +79,35 @@ describe("Messages", function () { calledOnce.should.not.have.been.called; }).to.throw("expected spy to not have been called"); - expect(function () { + expect(stripQuotes(function () { calledOnce.should.not.have.been.calledOnce; - }).to.throw("expected spy to not have been called exactly once"); + })).to.throw("expected spy to not have been called exactly once"); - expect(function () { + expect(stripQuotes(function () { calledTwice.should.not.have.been.calledTwice; - }).to.throw("expected spy to not have been called exactly twice"); + })).to.throw("expected spy to not have been called exactly twice"); - expect(function () { + expect(stripQuotes(function () { calledThrice.should.not.have.been.calledThrice; - }).to.throw("expected spy to not have been called exactly thrice"); + })).to.throw("expected spy to not have been called exactly thrice"); - expect(function () { + expect(stripQuotes(function () { calledOnce.should.not.have.callCount(1); - }).to.throw("expected spy to not have been called exactly once"); + })).to.throw("expected spy to not have been called exactly once"); - expect(function () { + expect(stripQuotes(function () { calledFourTimes.should.not.have.callCount(4); - }).to.throw("expected spy to not have been called exactly 4 times"); + })).to.throw("expected spy to not have been called exactly 4 times"); }); }); describe("about call order", function () { + function calledRegex(func, criteria, otherFunc) { + return new RegExp( + "expected " + func.displayName + " to " + criteria + + " (function " + otherFunc.displayName + "\\(\\) \\{\\}|\\[Function.*\\])" + ); + } it("should be correct for the base cases", function () { var spyA = sinon.spy(); var spyB = sinon.spy(); @@ -90,22 +117,22 @@ describe("Messages", function () { expect(function () { spyA.should.have.been.calledBefore(spyB); - }).to.throw("expected spyA to have been called before function spyB() {}"); + }).to.throw(calledRegex(spyA, "have been called before", spyB)); if (spyA.calledImmediatelyBefore) { expect(function () { spyA.should.have.been.calledImmediatelyBefore(spyB); - }).to.throw("expected spyA to have been called immediately before function spyB() {}"); + }).to.throw(calledRegex(spyA, "have been called immediately before", spyB)); } expect(function () { spyB.should.have.been.calledAfter(spyA); - }).to.throw("expected spyB to have been called after function spyA() {}"); + }).to.throw(calledRegex(spyB, "have been called after", spyA)); if (spyB.calledImmediatelyAfter) { expect(function () { spyB.should.have.been.calledImmediatelyAfter(spyA); - }).to.throw("expected spyB to have been called immediately after function spyA() {}"); + }).to.throw(calledRegex(spyB, "have been called immediately after", spyA)); } }); @@ -121,22 +148,22 @@ describe("Messages", function () { expect(function () { spyA.should.not.have.been.calledBefore(spyB); - }).to.throw("expected spyA to not have been called before function spyB() {}"); + }).to.throw(calledRegex(spyA, "not have been called before", spyB)); if (spyA.calledImmediatelyBefore) { expect(function () { spyA.should.not.have.been.calledImmediatelyBefore(spyB); - }).to.throw("expected spyA to not have been called immediately before function spyB() {}"); + }).to.throw(calledRegex(spyA, "not have been called immediately before", spyB)); } expect(function () { spyB.should.not.have.been.calledAfter(spyA); - }).to.throw("expected spyB to not have been called after function spyA() {}"); + }).to.throw(calledRegex(spyB, "not have been called after", spyA)); if (spyB.calledImmediatelyAfter) { expect(function () { spyB.should.not.have.been.calledImmediatelyAfter(spyA); - }).to.throw("expected spyB to not have been called immediately after function spyA() {}"); + }).to.throw(calledRegex(spyB, "not have been called immediately after", spyA)); } }); }); @@ -149,8 +176,10 @@ describe("Messages", function () { spy.call(badContext); - var expected = "expected spy to have been called with { } as this, but it was called with " + - spy.printf("%t") + " instead"; + var expected = new RegExp( + "expected spy to have been called with \\{\\s*\\} as this, but it was called with " + + spy.printf("%t") + " instead" + ); expect(function () { spy.should.have.been.calledOn(context); }).to.throw(expected); @@ -165,7 +194,7 @@ describe("Messages", function () { spy.call(context); - var expected = "expected spy to not have been called with { } as this"; + var expected = /expected spy to not have been called with \{\s*\} as this/; expect(function () { spy.should.not.have.been.calledOn(context); }).to.throw(expected); @@ -181,8 +210,10 @@ describe("Messages", function () { spy.call(badContext); - var expected = "expected spy to always have been called with { } as this, but it was called with " + - spy.printf("%t") + " instead"; + var expected = new RegExp( + "expected spy to always have been called with \\{\\s*\\} as this, but it was called with " + + spy.printf("%t") + " instead" + ); expect(function () { spy.should.always.have.been.calledOn(context); }).to.throw(expected); @@ -239,31 +270,31 @@ describe("Messages", function () { spy(1, 2, 3); - expect(function () { + expect(stripQuotesAndColors(function () { spy.should.have.been.calledWith("a", "b", "c"); - }).to.throw("expected spy to have been called with arguments a, b, c"); - expect(function () { + })).to.throw("expected spy to have been called with arguments \n1 a \n2 b \n3 c"); + expect(stripQuotesAndColors(function () { spy.should.have.been.calledWithExactly("a", "b", "c"); - }).to.throw("expected spy to have been called with exact arguments a, b, c"); - expect(function () { + })).to.throw("expected spy to have been called with exact arguments \n1 a \n2 b \n3 c"); + expect(stripColors(function () { spy.should.have.been.calledWithMatch(sinon.match("foo")); - }).to.throw("expected spy to have been called with arguments matching match(\"foo\")"); - expect(function () { + })).to.throw("expected spy to have been called with arguments matching \n1 match(\"foo\")"); + expect(stripQuotesAndColors(function () { spy.should.have.been.calledOnceWith("a", "b", "c"); - }).to.throw("expected spy to have been called exactly once with arguments a, b, c"); - expect(function () { + })).to.throw("expected spy to have been called exactly once with arguments \n1 a \n2 b \n3 c"); + expect(stripQuotesAndColors(function () { spy.should.have.been.calledOnceWithExactly("a", "b", "c"); - }).to.throw("expected spy to have been called exactly once with exact arguments a, b, c"); + })).to.throw("expected spy to have been called exactly once with exact arguments \n1 a \n2 b \n3 c"); - expect(function () { + expect(stripQuotesAndColors(function () { spy.getCall(0).should.have.been.calledWith("a", "b", "c"); - }).to.throw("expected spy to have been called with arguments a, b, c"); - expect(function () { + })).to.throw("expected spy to have been called with arguments \n1 a \n2 b \n3 c"); + expect(stripQuotesAndColors(function () { spy.getCall(0).should.have.been.calledWithExactly("a", "b", "c"); - }).to.throw("expected spy to have been called with exact arguments a, b, c"); - expect(function () { + })).to.throw("expected spy to have been called with exact arguments \n1 a \n2 b \n3 c"); + expect(stripColors(function () { spy.getCall(0).should.have.been.calledWithMatch(sinon.match("foo")); - }).to.throw("expected spy to have been called with arguments matching match(\"foo\")"); + })).to.throw("expected spy to have been called with arguments matching \n1 match(\"foo\")"); }); it("should be correct for the negated cases", function () { @@ -279,7 +310,7 @@ describe("Messages", function () { }).to.throw("expected spy to not have been called with exact arguments 1, 2, 3"); expect(function () { spy.should.not.have.been.calledWithMatch(sinon.match(1)); - }).to.throw("expected spy to not have been called with arguments matching match(1)"); + }).to.throw(/expected spy to not have been called with arguments matching.*match\(1\)/); expect(function () { spy.should.not.have.been.calledOnceWith(1, 2, 3); }).to.throw("expected spy to not have been called exactly once with arguments 1, 2, 3"); @@ -295,7 +326,7 @@ describe("Messages", function () { }).to.throw("expected spy to not have been called with exact arguments 1, 2, 3"); expect(function () { spy.getCall(0).should.not.have.been.calledWithMatch(sinon.match(1)); - }).to.throw("expected spy to not have been called with arguments matching match(1)"); + }).to.throw(/expected spy to not have been called with arguments matching.*match\(1\)/); }); it("should be correct for the always cases", function () { @@ -304,42 +335,72 @@ describe("Messages", function () { spy(1, 2, 3); spy("a", "b", "c"); - var expected = /expected spy to always have been called with arguments 1, 2, 3/; - expect(function () { + var expected = new RegExp( + "expected spy to always have been called with arguments.*" + + "Call 2:\\na 1 \\nb 2 \\nc 3", + "s" + ); + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledWith(1, 2, 3); - }).to.throw(expected); - - var expectedExactly = /expected spy to always have been called with exact arguments 1, 2, 3/; - expect(function () { + })).to.throw(expected); + + var expectedExactly = new RegExp( + "expected spy to always have been called with exact arguments.*" + + "Call 2:\\na 1 \\nb 2 \\nc 3", + "s" + ); + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledWithExactly(1, 2, 3); - }).to.throw(expectedExactly); - - var expectedMatch = /expected spy to always have been called with arguments matching match\(1\)/; - expect(function () { + })).to.throw(expectedExactly); + + var expectedMatch = new RegExp( + "expected spy to always have been called with arguments matching.*" + + "Call 2:\\na match\\(1\\)", + "s" + ); + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledWithMatch(sinon.match(1)); - }).to.throw(expectedMatch); - - var expectedOnce = /expected spy to have been called exactly once with arguments 1, 2, 3/; - expect(function () { + })).to.throw(expectedMatch); + + var expectedOnce = new RegExp( + "expected spy to have been called exactly once with arguments.*" + + "Call 2:\\na 1 \\nb 2 \\nc 3", + "s" + ); + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledOnceWith(1, 2, 3); - }).to.throw(expectedOnce); - - var expectedExactlyOnce = /expected spy to have been called exactly once with exact arguments 1, 2, 3/; - expect(function () { + })).to.throw(expectedOnce); + + var expectedExactlyOnce = new RegExp( + "expected spy to have been called exactly once with exact arguments.*" + + "Call 2:\\na 1 \\nb 2 \\nc 3 ", + "s" + ); + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledOnceWithExactly(1, 2, 3); - }).to.throw(expectedExactlyOnce); + })).to.throw(expectedExactlyOnce); spy.resetHistory(); spy(1, 2, 3); spy(1, 2, 3); - expect(function () { + var expectedOnceButTwice = new RegExp( + "expected spy to have been called exactly once with arguments.*" + + "Call 2:\\n1\\n2\\n3", + "s" + ); + expect(stripColors(function () { spy.should.always.have.been.calledOnceWith(1, 2, 3); - }).to.throw(expectedOnce); - - expect(function () { + })).to.throw(expectedOnceButTwice); + + var expectedExactlyOnceButTwice = new RegExp( + "expected spy to have been called exactly once with exact arguments.*" + + "Call 2:\\n1\\n2\\n3", + "s" + ); + expect(stripColors(function () { spy.should.always.have.been.calledOnceWithExactly(1, 2, 3); - }).to.throw(expectedExactlyOnce); + })).to.throw(expectedExactlyOnceButTwice); }); }); @@ -404,19 +465,19 @@ describe("Messages", function () { spy.getCall(0).should.have.thrown(); }).to.throw("expected spy to have thrown"); - expect(function () { + expect(stripQuotes(function () { throwingSpy.should.have.thrown("TypeError"); - }).to.throw("expected spy to have thrown TypeError"); - expect(function () { + })).to.throw("expected spy to have thrown TypeError"); + expect(stripQuotes(function () { throwingSpy.getCall(0).should.have.thrown("TypeError"); - }).to.throw("expected spy to have thrown TypeError"); + })).to.throw("expected spy to have thrown TypeError"); expect(function () { throwingSpy.should.have.thrown({ message: "x" }); - }).to.throw('expected spy to have thrown { message: "x" }'); + }).to.throw(/expected spy to have thrown { message: ['"]x['"] }/); expect(function () { throwingSpy.getCall(0).should.have.thrown({ message: "x" }); - }).to.throw('expected spy to have thrown { message: "x" }'); + }).to.throw(/expected spy to have thrown { message: ['"]x['"] }/); }); it("should be correct for the negated cases", function () { @@ -434,12 +495,12 @@ describe("Messages", function () { spy.getCall(0).should.not.have.thrown(); }).to.throw("expected spy to not have thrown"); - expect(function () { + expect(stripQuotes(function () { spy.should.not.have.thrown("Error"); - }).to.throw("expected spy to not have thrown Error"); - expect(function () { + })).to.throw("expected spy to not have thrown Error"); + expect(stripQuotes(function () { spy.getCall(0).should.not.have.thrown("Error"); - }).to.throw("expected spy to not have thrown Error"); + })).to.throw("expected spy to not have thrown Error"); expect(function () { spy.should.not.have.thrown(error); @@ -462,13 +523,13 @@ describe("Messages", function () { spy.should.have.always.thrown(); }).to.throw("expected spy to always have thrown"); - expect(function () { + expect(stripQuotes(function () { throwingSpy.should.have.always.thrown("TypeError"); - }).to.throw("expected spy to always have thrown TypeError"); + })).to.throw("expected spy to always have thrown TypeError"); expect(function () { throwingSpy.should.have.always.thrown({ message: "x" }); - }).to.throw('expected spy to always have thrown { message: "x" }'); + }).to.throw(/expected spy to always have thrown { message: ['"]x['"] }/); }); }); @@ -480,13 +541,13 @@ describe("Messages", function () { it("should be informative for properties", function () { expect(function () { notSpy.should.have.been.called; - }).to.throw(TypeError, /not a spy/); + }).to.throw(TypeError, "not a spy"); }); it("should be informative for methods", function () { expect(function () { notSpy.should.have.been.calledWith("foo"); - }).to.throw(TypeError, /not a spy/); + }).to.throw(TypeError, "not a spy"); }); });