From 21d47f2ff927986839b95e1065201eb5a4a6d51c Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Sat, 21 Oct 2023 12:13:58 +0200 Subject: [PATCH] Refactoring: move logic into separate file and rename This is basically a small cleanup of the work done to get rid of Proxyquire before the release of version 17. - Pull out Sinon extraction to own file to avoid needless export on the root Sinon object - Rename file to match Colorizer class --- lib/create-sinon-api.js | 56 ++++++++++++++++++++++++++ lib/sinon.js | 59 +--------------------------- lib/sinon/{color.js => colorizer.js} | 6 +++ lib/sinon/spy-formatters.js | 12 +++--- test/assert-test.js | 2 +- test/proxy-test.js | 2 +- test/sinon-test.js | 2 +- test/util/core/color-test.js | 2 +- 8 files changed, 74 insertions(+), 67 deletions(-) create mode 100644 lib/create-sinon-api.js rename lib/sinon/{color.js => colorizer.js} (83%) diff --git a/lib/create-sinon-api.js b/lib/create-sinon-api.js new file mode 100644 index 000000000..24cd0f77a --- /dev/null +++ b/lib/create-sinon-api.js @@ -0,0 +1,56 @@ +"use strict"; + +const behavior = require("./sinon/behavior"); +const createSandbox = require("./sinon/create-sandbox"); +const extend = require("./sinon/util/core/extend"); +const fakeTimers = require("./sinon/util/fake-timers"); +const Sandbox = require("./sinon/sandbox"); +const stub = require("./sinon/stub"); +const promise = require("./sinon/promise"); +const assert = require("node:assert"); + +/** + * @param {object} opts injection point to override the default XHR lib in testing + * @param {object} opts.sinonXhrLib + * @returns {object} a configured sandbox + */ +module.exports = function createApi({ sinonXhrLib }) { + assert(sinonXhrLib, "No XHR lib passed in"); + + const apiMethods = { + createSandbox: createSandbox, + assert: require("./sinon/assert"), + match: require("@sinonjs/samsam").createMatcher, + restoreObject: require("./sinon/restore-object"), + + expectation: require("./sinon/mock-expectation"), + defaultConfig: require("./sinon/util/core/default-config"), + + // fake timers + timers: fakeTimers.timers, + + // fake XHR + xhr: sinonXhrLib.fakeXhr.xhr, + FakeXMLHttpRequest: sinonXhrLib.fakeXhr.FakeXMLHttpRequest, + + // fake server + fakeServer: sinonXhrLib.fakeServer, + fakeServerWithClock: sinonXhrLib.fakeServerWithClock, + createFakeServer: sinonXhrLib.fakeServer.create.bind( + sinonXhrLib.fakeServer, + ), + createFakeServerWithClock: sinonXhrLib.fakeServerWithClock.create.bind( + sinonXhrLib.fakeServerWithClock, + ), + + addBehavior: function (name, fn) { + behavior.addBehavior(stub, name, fn); + }, + + // fake promise + promise: promise, + }; + + const sandbox = new Sandbox(); + return extend(sandbox, apiMethods); +}; diff --git a/lib/sinon.js b/lib/sinon.js index bb735342f..271477b65 100644 --- a/lib/sinon.js +++ b/lib/sinon.js @@ -1,61 +1,6 @@ "use strict"; -const behavior = require("./sinon/behavior"); -const createSandbox = require("./sinon/create-sandbox"); -const extend = require("./sinon/util/core/extend"); -const fakeTimers = require("./sinon/util/fake-timers"); const nise = require("nise"); -const Sandbox = require("./sinon/sandbox"); -const stub = require("./sinon/stub"); -const promise = require("./sinon/promise"); +const createApi = require("./create-sinon-api"); -/** - * @param {object} opts injection point to override the default XHR lib in testing - * @param {object} opts.sinonXhrLib - * @returns {object} a configured sandbox - */ -function createApi({ sinonXhrLib }) { - const apiMethods = { - createSandbox: createSandbox, - assert: require("./sinon/assert"), - match: require("@sinonjs/samsam").createMatcher, - restoreObject: require("./sinon/restore-object"), - - expectation: require("./sinon/mock-expectation"), - defaultConfig: require("./sinon/util/core/default-config"), - - // fake timers - timers: fakeTimers.timers, - - // fake XHR - xhr: sinonXhrLib.fakeXhr.xhr, - FakeXMLHttpRequest: sinonXhrLib.fakeXhr.FakeXMLHttpRequest, - - // fake server - fakeServer: sinonXhrLib.fakeServer, - fakeServerWithClock: sinonXhrLib.fakeServerWithClock, - createFakeServer: sinonXhrLib.fakeServer.create.bind( - sinonXhrLib.fakeServer, - ), - createFakeServerWithClock: sinonXhrLib.fakeServerWithClock.create.bind( - sinonXhrLib.fakeServerWithClock, - ), - - addBehavior: function (name, fn) { - behavior.addBehavior(stub, name, fn); - }, - - // fake promise - promise: promise, - }; - - const sandbox = new Sandbox(); - return extend(sandbox, apiMethods); -} - -const api = createApi({ sinonXhrLib: nise }); - -module.exports = api; - -// solely exposed for easier testing -module.exports.createApi = createApi; +module.exports = createApi({ sinonXhrLib: nise }); diff --git a/lib/sinon/color.js b/lib/sinon/colorizer.js similarity index 83% rename from lib/sinon/color.js rename to lib/sinon/colorizer.js index 6ddb1acb9..242517990 100644 --- a/lib/sinon/color.js +++ b/lib/sinon/colorizer.js @@ -5,6 +5,12 @@ module.exports = class Colorizer { this.supportsColor = supportsColor; } + /** + * Should be renamed to true #privateField + * when we can ensure ES2022 support + * + * @private + */ colorize(str, color) { if (this.supportsColor.stdout === false) { return str; diff --git a/lib/sinon/spy-formatters.js b/lib/sinon/spy-formatters.js index 919771914..d5e67fae0 100644 --- a/lib/sinon/spy-formatters.js +++ b/lib/sinon/spy-formatters.js @@ -1,8 +1,8 @@ "use strict"; const arrayProto = require("@sinonjs/commons").prototypes.array; -const Colorizer = require("./color"); -const color = new Colorizer(); +const Colorizer = require("./colorizer"); +const colororizer = new Colorizer(); const match = require("@sinonjs/samsam").createMatcher; const timesInWords = require("./util/core/times-in-words"); const inspect = require("util").inspect; @@ -25,9 +25,9 @@ function colorSinonMatchText(matcher, calledArg, calledArgMessage) { let calledArgumentMessage = calledArgMessage; let matcherMessage = matcher.message; if (!matcher.test(calledArg)) { - matcherMessage = color.red(matcher.message); + matcherMessage = colororizer.red(matcher.message); if (calledArgumentMessage) { - calledArgumentMessage = color.green(calledArgumentMessage); + calledArgumentMessage = colororizer.green(calledArgumentMessage); } } return `${calledArgumentMessage} ${matcherMessage}`; @@ -42,9 +42,9 @@ function colorDiffText(diff) { const objects = map(diff, function (part) { let text = part.value; if (part.added) { - text = color.green(text); + text = colororizer.green(text); } else if (part.removed) { - text = color.red(text); + text = colororizer.red(text); } if (diff.length === 2) { text += " "; // format simple diffs diff --git a/test/assert-test.js b/test/assert-test.js index 1f3c8e4bb..036dc2231 100644 --- a/test/assert-test.js +++ b/test/assert-test.js @@ -1,6 +1,6 @@ "use strict"; -const Colorizer = require("../lib/sinon/color"); +const Colorizer = require("../lib/sinon/colorizer"); const color = new Colorizer(); const referee = require("@sinonjs/referee"); const sinonStub = require("../lib/sinon/stub"); diff --git a/test/proxy-test.js b/test/proxy-test.js index 05226ae4b..c74eec101 100644 --- a/test/proxy-test.js +++ b/test/proxy-test.js @@ -4,7 +4,7 @@ const assert = require("@sinonjs/referee").assert; const extend = require("../lib/sinon/util/core/extend"); const createProxy = require("../lib/sinon/proxy"); -const Colorizer = require("../lib/sinon/color"); +const Colorizer = require("../lib/sinon/colorizer"); const color = new Colorizer(); const sinonSpy = require("../lib/sinon/spy"); const sinonStub = require("../lib/sinon/stub"); diff --git a/test/sinon-test.js b/test/sinon-test.js index 386e27053..64b61f1fb 100644 --- a/test/sinon-test.js +++ b/test/sinon-test.js @@ -31,7 +31,7 @@ describe("sinon module", function () { useFakeXMLHttpRequest: "ba8bd609-c921-4a62-a1b9-49336bd426a4", }, }; - sinon = require("../lib/sinon").createApi({ + sinon = require("../lib/create-sinon-api")({ sinonXhrLib: fakeNise, }); }); diff --git a/test/util/core/color-test.js b/test/util/core/color-test.js index 3a9ab8d9d..3afc90e1c 100644 --- a/test/util/core/color-test.js +++ b/test/util/core/color-test.js @@ -1,7 +1,7 @@ "use strict"; const assert = require("@sinonjs/referee").assert; -const Colorizer = require("../../../lib/sinon/color"); +const Colorizer = require("../../../lib/sinon/colorizer"); const colors = [ { name: "bold", code: 1 },