Skip to content

Commit

Permalink
Refactoring: move logic into separate file and rename
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fatso83 committed Oct 21, 2023
1 parent 8dbfd02 commit 21d47f2
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 67 deletions.
56 changes: 56 additions & 0 deletions lib/create-sinon-api.js
Original file line number Diff line number Diff line change
@@ -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);
};
59 changes: 2 additions & 57 deletions lib/sinon.js
Original file line number Diff line number Diff line change
@@ -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 });
6 changes: 6 additions & 0 deletions lib/sinon/color.js → lib/sinon/colorizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions lib/sinon/spy-formatters.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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}`;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/assert-test.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
2 changes: 1 addition & 1 deletion test/proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion test/sinon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/util/core/color-test.js
Original file line number Diff line number Diff line change
@@ -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 },
Expand Down

0 comments on commit 21d47f2

Please sign in to comment.