From 13f1d92cc6ebf5e24039056c2777ccb4e71a3761 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Mon, 1 Nov 2021 11:44:14 -0700 Subject: [PATCH 01/10] Remove unnecessary arguments from non-negated cases This is a continuation in the spirit of #104, but resolves the awkwardness with the negative version, and brings us closer to core Sinon messages. To do this, we changed the nonNegatedSuffix parameter to instead take a pair of suffixes, nonNegated and negated. This allows us to specify %D for the nonNegated case, which eliminates noisy duplication, while still using %*%C for the negated case. This also matches what core Sinon outputs in these cases. As shorthand, we still support specifying a single value, which we use for nonNegatedSuffix and assume an empty negatedSuffix. --- lib/sinon-chai.js | 51 ++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/sinon-chai.js b/lib/sinon-chai.js index 194dc3a..b391d20 100644 --- a/lib/sinon-chai.js +++ b/lib/sinon-chai.js @@ -54,9 +54,20 @@ } } - 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, nonNegatedSuffix + if (Array.isArray(suffixes)) { + nonNegatedSuffix = suffixes[0] || "" + negatedSuffix = suffixes[1] || "" + } else { + nonNegatedSuffix = suffixes || "" + negatedSuffix = "" + } + if (isSpy(spy.proxy)) { spy = spy.proxy; } @@ -70,30 +81,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 +112,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 +121,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 +150,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"); })); From edd501111bacec70bdeeacf08dcde3e5fba28892 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Mon, 1 Nov 2021 12:05:37 -0700 Subject: [PATCH 02/10] Update package-lock for version bumps --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index cda1e57..55146f8 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": { From ffff445ab34b7694cefc9836ecff0f7745643711 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Mon, 1 Nov 2021 13:04:43 -0700 Subject: [PATCH 03/10] Update tests for new less duplicative messages I'm moderately happy with this. It got a little messy because now we can't depend on the brief but duplicative listing of arguments, and have to match against the diff view, which is colorized and has newlines and things. I pulled in strip-ansi@6 because it's already a transitive dependency, and added a little wrapper function to rewrite the error messages being thrown. The wrapper is a little weird, but was the best solution I came up with. Other things I tried or considered: - Disable color output (only possible by manipulating process.env or process.argv, both of which seem like a no-go) - Create a custom matcher that strips the message before comparing (throw() doesn't support matchers) - Matching color escapes with a RegExp (seemed way messier than this) - Manually try/catch, store error, assert against the error (also seemed messier than this) The wrapper could be elsewhere too - it could be a wrapper that calls expect(), for instance - but I think this is clear enough. --- package-lock.json | 114 ++++++++++++++++++++++++++++++++++++++----- package.json | 3 +- test/messages.js | 122 +++++++++++++++++++++++++++++++--------------- 3 files changed, 186 insertions(+), 53 deletions(-) diff --git a/package-lock.json b/package-lock.json index 55146f8..f940282 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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..70689f1 100644 --- a/test/messages.js +++ b/test/messages.js @@ -3,6 +3,18 @@ var sinon = require("sinon"); var expect = require("chai").expect; var swallow = require("./common").swallow; +var stripAnsi = require("strip-ansi"); + +function stripMessageColors(func) { + return function () { + try { + func(); + } catch (error) { + error.message = stripAnsi(error.message); + throw error; + } + }; +} describe("Messages", function () { describe("about call count", function () { @@ -239,31 +251,31 @@ describe("Messages", function () { spy(1, 2, 3); - expect(function () { + expect(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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 () { @@ -304,42 +316,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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(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(stripMessageColors(function () { spy.should.always.have.been.calledOnceWithExactly(1, 2, 3); - }).to.throw(expectedExactlyOnce); + })).to.throw(expectedExactlyOnceButTwice); }); }); From 38d67bf01fecf07e8328e922007efd4a17af218c Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Mon, 1 Nov 2021 14:21:00 -0700 Subject: [PATCH 04/10] Lint fixes --- lib/sinon-chai.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/sinon-chai.js b/lib/sinon-chai.js index b391d20..4bbdf11 100644 --- a/lib/sinon-chai.js +++ b/lib/sinon-chai.js @@ -59,13 +59,14 @@ // Suffixes is [nonNegatedSuffix, negatedSuffix], or just // nonNegatedSuffix if there is no negatedSuffix - var negatedSuffix, nonNegatedSuffix + var negatedSuffix; + var nonNegatedSuffix; if (Array.isArray(suffixes)) { - nonNegatedSuffix = suffixes[0] || "" - negatedSuffix = suffixes[1] || "" + nonNegatedSuffix = suffixes[0] || ""; + negatedSuffix = suffixes[1] || ""; } else { - nonNegatedSuffix = suffixes || "" - negatedSuffix = "" + nonNegatedSuffix = suffixes || ""; + negatedSuffix = ""; } if (isSpy(spy.proxy)) { From d086d5836ded42be2a076ad2c4672cba767c30e0 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Mon, 1 Nov 2021 15:23:29 -0700 Subject: [PATCH 05/10] Deal with quoting differences in recent versions of Sinon This was broken before this PR, but might as well fix it --- test/messages.js | 62 ++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/test/messages.js b/test/messages.js index 70689f1..aef502e 100644 --- a/test/messages.js +++ b/test/messages.js @@ -23,30 +23,30 @@ describe("Messages", function () { expect(function () { spy.should.have.been.called; - }).to.throw("expected spy to have been called at least once, but it was never called"); + }).to.throw(/expected spy to have been called at least '?once'?, but it was never called/); expect(function () { spy.should.have.been.calledOnce; - }).to.throw("expected spy to have been called exactly once, but it was called 0 times"); + }).to.throw(/expected spy to have been called exactly '?once'?, but it was called 0 times/); expect(function () { spy.should.have.been.calledTwice; - }).to.throw("expected spy to have been called exactly twice, but it was called 0 times"); + }).to.throw(/expected spy to have been called exactly '?twice'?, but it was called 0 times/); expect(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 () { spy.should.have.callCount(1); - }).to.throw("expected spy to have been called exactly once, but it was called 0 times"); + }).to.throw(/expected spy to have been called exactly '?once'?, but it was called 0 times/); expect(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 () { spy.should.have.been.calledOnceWith(); - }).to.throw("expected spy to have been called exactly once with arguments"); + }).to.throw(/expected spy to have been called exactly '?once'? with arguments/); expect(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 () { @@ -72,23 +72,23 @@ describe("Messages", function () { expect(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 () { 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 () { 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 () { 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 () { 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'?/); }); }); @@ -262,10 +262,10 @@ describe("Messages", function () { })).to.throw("expected spy to have been called with arguments matching \n1 match(\"foo\")"); expect(stripMessageColors(function () { spy.should.have.been.calledOnceWith("a", "b", "c"); - })).to.throw("expected spy to have been called exactly once with arguments \n1 a \n2 b \n3 c"); + })).to.throw(/expected spy to have been called exactly '?once'? with arguments \n1 a \n2 b \n3 c/); expect(stripMessageColors(function () { spy.should.have.been.calledOnceWithExactly("a", "b", "c"); - })).to.throw("expected spy to have been called exactly once with exact arguments \n1 a \n2 b \n3 c"); + })).to.throw(/expected spy to have been called exactly '?once'? with exact arguments \n1 a \n2 b \n3 c/); expect(stripMessageColors(function () { spy.getCall(0).should.have.been.calledWith("a", "b", "c"); @@ -294,10 +294,10 @@ describe("Messages", function () { }).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"); + }).to.throw(/expected spy to not have been called exactly '?once'? with arguments 1, 2, 3/); expect(function () { spy.should.not.have.been.calledOnceWithExactly(1, 2, 3); - }).to.throw("expected spy to not have been called exactly once with exact arguments 1, 2, 3"); + }).to.throw(/expected spy to not have been called exactly '?once'? with exact arguments 1, 2, 3/); expect(function () { spy.getCall(0).should.not.have.been.calledWith(1, 2, 3); @@ -344,7 +344,7 @@ describe("Messages", function () { })).to.throw(expectedMatch); var expectedOnce = new RegExp( - "expected spy to have been called exactly once with arguments.*" + + "expected spy to have been called exactly '?once'? with arguments.*" + "Call 2:\\na 1 \\nb 2 \\nc 3", "s" ); @@ -353,7 +353,7 @@ describe("Messages", function () { })).to.throw(expectedOnce); var expectedExactlyOnce = new RegExp( - "expected spy to have been called exactly once with exact arguments.*" + + "expected spy to have been called exactly '?once'? with exact arguments.*" + "Call 2:\\na 1 \\nb 2 \\nc 3 ", "s" ); @@ -366,7 +366,7 @@ describe("Messages", function () { spy(1, 2, 3); var expectedOnceButTwice = new RegExp( - "expected spy to have been called exactly once with arguments.*" + + "expected spy to have been called exactly '?once'? with arguments.*" + "Call 2:\\n1\\n2\\n3", "s" ); @@ -375,7 +375,7 @@ describe("Messages", function () { })).to.throw(expectedOnceButTwice); var expectedExactlyOnceButTwice = new RegExp( - "expected spy to have been called exactly once with exact arguments.*" + + "expected spy to have been called exactly '?once'? with exact arguments.*" + "Call 2:\\n1\\n2\\n3", "s" ); @@ -448,17 +448,17 @@ describe("Messages", function () { expect(function () { throwingSpy.should.have.thrown("TypeError"); - }).to.throw("expected spy to have thrown TypeError"); + }).to.throw(/expected spy to have thrown '?TypeError'?/); expect(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 () { @@ -478,17 +478,17 @@ describe("Messages", function () { expect(function () { spy.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.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); - }).to.throw("expected spy to not have thrown Error: boo!"); + }).to.throw(/expected spy to not have thrown '?Error: boo!'?/); expect(function () { spy.getCall(0).should.not.have.thrown(error); - }).to.throw("expected spy to not have thrown Error: boo!"); + }).to.throw(/expected spy to not have thrown '?Error: boo!'?/); }); it("should be correct for the always cases", function () { @@ -506,11 +506,11 @@ describe("Messages", function () { expect(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['"] }/); }); }); From 2e24e7fb6eab42e819423a70971f2ec98eed724e Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Mon, 1 Nov 2021 15:47:32 -0700 Subject: [PATCH 06/10] Fix for varying brace spacing in different Sinon versions --- test/messages.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/messages.js b/test/messages.js index aef502e..a1d56fb 100644 --- a/test/messages.js +++ b/test/messages.js @@ -161,8 +161,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); @@ -177,7 +179,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); @@ -193,8 +195,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); From de3e205cdaed8779fe497c23497da6084e334cad Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Mon, 1 Nov 2021 16:29:48 -0700 Subject: [PATCH 07/10] Strip quotes instead of trying to regex them --- test/messages.js | 169 +++++++++++++++++++++++++---------------------- 1 file changed, 89 insertions(+), 80 deletions(-) diff --git a/test/messages.js b/test/messages.js index a1d56fb..855b2eb 100644 --- a/test/messages.js +++ b/test/messages.js @@ -5,48 +5,57 @@ var expect = require("chai").expect; var swallow = require("./common").swallow; var stripAnsi = require("strip-ansi"); -function stripMessageColors(func) { - return function () { - try { - func(); - } catch (error) { - error.message = stripAnsi(error.message); - throw error; - } +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 () { @@ -70,25 +79,25 @@ 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"); }); }); @@ -255,29 +264,29 @@ describe("Messages", function () { spy(1, 2, 3); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.should.have.been.calledWith("a", "b", "c"); })).to.throw("expected spy to have been called with arguments \n1 a \n2 b \n3 c"); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.should.have.been.calledWithExactly("a", "b", "c"); })).to.throw("expected spy to have been called with exact arguments \n1 a \n2 b \n3 c"); - expect(stripMessageColors(function () { + expect(stripColors(function () { spy.should.have.been.calledWithMatch(sinon.match("foo")); })).to.throw("expected spy to have been called with arguments matching \n1 match(\"foo\")"); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.should.have.been.calledOnceWith("a", "b", "c"); - })).to.throw(/expected spy to have been called exactly '?once'? with arguments \n1 a \n2 b \n3 c/); - expect(stripMessageColors(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 \n1 a \n2 b \n3 c/); + })).to.throw("expected spy to have been called exactly once with exact arguments \n1 a \n2 b \n3 c"); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.getCall(0).should.have.been.calledWith("a", "b", "c"); })).to.throw("expected spy to have been called with arguments \n1 a \n2 b \n3 c"); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.getCall(0).should.have.been.calledWithExactly("a", "b", "c"); })).to.throw("expected spy to have been called with exact arguments \n1 a \n2 b \n3 c"); - expect(stripMessageColors(function () { + expect(stripColors(function () { spy.getCall(0).should.have.been.calledWithMatch(sinon.match("foo")); })).to.throw("expected spy to have been called with arguments matching \n1 match(\"foo\")"); }); @@ -295,13 +304,13 @@ 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/); + }).to.throw("expected spy to not have been called exactly once with arguments 1, 2, 3"); expect(function () { spy.should.not.have.been.calledOnceWithExactly(1, 2, 3); - }).to.throw(/expected spy to not have been called exactly '?once'? with exact arguments 1, 2, 3/); + }).to.throw("expected spy to not have been called exactly once with exact arguments 1, 2, 3"); expect(function () { spy.getCall(0).should.not.have.been.calledWith(1, 2, 3); @@ -311,7 +320,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 () { @@ -322,46 +331,46 @@ describe("Messages", function () { var expected = new RegExp( "expected spy to always have been called with arguments.*" + - "Call 2:\\na 1 \\nb 2 \\nc 3", + "Call 2:\\na 1 \\nb 2 \\nc 3", "s" ); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledWith(1, 2, 3); })).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", + "Call 2:\\na 1 \\nb 2 \\nc 3", "s" ); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledWithExactly(1, 2, 3); })).to.throw(expectedExactly); var expectedMatch = new RegExp( "expected spy to always have been called with arguments matching.*" + - "Call 2:\\na match\\(1\\)", + "Call 2:\\na match\\(1\\)", "s" ); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledWithMatch(sinon.match(1)); })).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", + "expected spy to have been called exactly once with arguments.*" + + "Call 2:\\na 1 \\nb 2 \\nc 3", "s" ); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledOnceWith(1, 2, 3); })).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 ", + "expected spy to have been called exactly once with exact arguments.*" + + "Call 2:\\na 1 \\nb 2 \\nc 3 ", "s" ); - expect(stripMessageColors(function () { + expect(stripQuotesAndColors(function () { spy.should.always.have.been.calledOnceWithExactly(1, 2, 3); })).to.throw(expectedExactlyOnce); @@ -370,20 +379,20 @@ describe("Messages", function () { spy(1, 2, 3); var expectedOnceButTwice = new RegExp( - "expected spy to have been called exactly '?once'? with arguments.*" + - "Call 2:\\n1\\n2\\n3", + "expected spy to have been called exactly once with arguments.*" + + "Call 2:\\n1\\n2\\n3", "s" ); - expect(stripMessageColors(function () { + expect(stripColors(function () { spy.should.always.have.been.calledOnceWith(1, 2, 3); })).to.throw(expectedOnceButTwice); var expectedExactlyOnceButTwice = new RegExp( - "expected spy to have been called exactly '?once'? with exact arguments.*" + - "Call 2:\\n1\\n2\\n3", + "expected spy to have been called exactly once with exact arguments.*" + + "Call 2:\\n1\\n2\\n3", "s" ); - expect(stripMessageColors(function () { + expect(stripColors(function () { spy.should.always.have.been.calledOnceWithExactly(1, 2, 3); })).to.throw(expectedExactlyOnceButTwice); }); @@ -450,12 +459,12 @@ 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" }); @@ -480,19 +489,19 @@ 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); - }).to.throw(/expected spy to not have thrown '?Error: boo!'?/); + }).to.throw("expected spy to not have thrown Error: boo!"); expect(function () { spy.getCall(0).should.not.have.thrown(error); - }).to.throw(/expected spy to not have thrown '?Error: boo!'?/); + }).to.throw("expected spy to not have thrown Error: boo!"); }); it("should be correct for the always cases", function () { @@ -508,9 +517,9 @@ 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" }); @@ -526,13 +535,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"); }); }); From c1cec446b7fc82138500eabefa7c6f566e27c0d4 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Mon, 1 Nov 2021 18:06:35 -0700 Subject: [PATCH 08/10] Workaround for Sinon bug that repeatedly quotes things I discovered this bug while working on this, there's a PR for fix here for reference: https://github.com/sinonjs/sinon/pull/2407 --- test/messages.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/messages.js b/test/messages.js index 855b2eb..db90d5f 100644 --- a/test/messages.js +++ b/test/messages.js @@ -18,11 +18,11 @@ function makeErrorMessageTransform(transform) { }; } var stripQuotes = makeErrorMessageTransform(function (message) { - return message.replace(/['"]/g, ""); + return message.replace(/\\*['"]/g, ""); }); var stripColors = makeErrorMessageTransform(stripAnsi); var stripQuotesAndColors = makeErrorMessageTransform(function (message) { - return stripAnsi(message).replace(/['"]/g, ""); + return stripAnsi(message).replace(/\\*['"]/g, ""); }); describe("Messages", function () { From e3fb6e02e4d3710f1612660f28b59d7849101754 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Mon, 1 Nov 2021 18:31:41 -0700 Subject: [PATCH 09/10] Account for argument formatting in newer Sinon versions This is a bit of a regression imo on Sinon's part, but I don't see an easy way to adjust for it --- test/messages.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/messages.js b/test/messages.js index db90d5f..879d695 100644 --- a/test/messages.js +++ b/test/messages.js @@ -102,6 +102,12 @@ describe("Messages", function () { }); 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(); @@ -111,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)); } }); @@ -142,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)); } }); }); From 40bc5e7af1a15c7c8538ddaed4788b10c3179135 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Tue, 2 Nov 2021 09:55:41 -0700 Subject: [PATCH 10/10] Account for differences in Node 14 inspect --- test/messages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/messages.js b/test/messages.js index 879d695..293bf14 100644 --- a/test/messages.js +++ b/test/messages.js @@ -105,7 +105,7 @@ describe("Messages", function () { function calledRegex(func, criteria, otherFunc) { return new RegExp( "expected " + func.displayName + " to " + criteria + - " (function " + otherFunc.displayName + "\\(\\) \\{\\}|\\[Function\\])" + " (function " + otherFunc.displayName + "\\(\\) \\{\\}|\\[Function.*\\])" ); } it("should be correct for the base cases", function () {