Skip to content

Commit

Permalink
Remove Proxyquire as it did not handle newer syntax
Browse files Browse the repository at this point in the history
The defaults of Prettier 3 threw Proxyquirify into a frenzy.
Did the minimal rewrite (DI) that worked
  • Loading branch information
fatso83 committed Oct 20, 2023
1 parent ec37d63 commit 11a8e3f
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 667 deletions.
83 changes: 48 additions & 35 deletions lib/sinon.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,53 @@ const Sandbox = require("./sinon/sandbox");
const stub = require("./sinon/stub");
const promise = require("./sinon/promise");

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: nise.fakeXhr.xhr,
FakeXMLHttpRequest: nise.fakeXhr.FakeXMLHttpRequest,

// fake server
fakeServer: nise.fakeServer,
fakeServerWithClock: nise.fakeServerWithClock,
createFakeServer: nise.fakeServer.create.bind(nise.fakeServer),
createFakeServerWithClock: nise.fakeServerWithClock.create.bind(
nise.fakeServerWithClock,
),

addBehavior: function (name, fn) {
behavior.addBehavior(stub, name, fn);
},

// fake promise
promise: promise,
};

const sandbox = new Sandbox();

const api = extend(sandbox, apiMethods);
/**
* @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;
46 changes: 25 additions & 21 deletions lib/sinon/color.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
"use strict";

const supportsColor = require("supports-color");

function colorize(str, color) {
if (supportsColor.stdout === false) {
return str;
module.exports = class Colorizer {
constructor(supportsColor = require("supports-color")) {
this.supportsColor = supportsColor;
}

return `\x1b[${color}m${str}\x1b[0m`;
}
colorize(str, color) {
if (this.supportsColor.stdout === false) {
return str;
}

exports.red = function (str) {
return colorize(str, 31);
};
return `\x1b[${color}m${str}\x1b[0m`;
}

exports.green = function (str) {
return colorize(str, 32);
};
red(str) {
return this.colorize(str, 31);
}

exports.cyan = function (str) {
return colorize(str, 96);
};
green(str) {
return this.colorize(str, 32);
}

exports.white = function (str) {
return colorize(str, 39);
};
cyan(str) {
return this.colorize(str, 96);
}

exports.bold = function (str) {
return colorize(str, 1);
white(str) {
return this.colorize(str, 39);
}

bold(str) {
return this.colorize(str, 1);
}
};
17 changes: 16 additions & 1 deletion lib/sinon/spy-formatters.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

const arrayProto = require("@sinonjs/commons").prototypes.array;
const color = require("./color");
const Colorizer = require("./color");
const color = new Colorizer();
const match = require("@sinonjs/samsam").createMatcher;
const timesInWords = require("./util/core/times-in-words");
const inspect = require("util").inspect;
Expand All @@ -12,6 +13,12 @@ const map = arrayProto.map;
const push = arrayProto.push;
const slice = arrayProto.slice;

/**
*
* @param matcher
* @param calledArg
* @param calledArgMessage
*/
function colorSinonMatchText(matcher, calledArg, calledArgMessage) {
let calledArgumentMessage = calledArgMessage;
let matcherMessage = matcher.message;
Expand All @@ -24,6 +31,10 @@ function colorSinonMatchText(matcher, calledArg, calledArgMessage) {
return `${calledArgumentMessage} ${matcherMessage}`;
}

/**
*
* @param diff
*/
function colorDiffText(diff) {
const objects = map(diff, function (part) {
let text = part.value;
Expand All @@ -40,6 +51,10 @@ function colorDiffText(diff) {
return join(objects, "");
}

/**
*
* @param value
*/
function quoteStringValue(value) {
if (typeof value === "string") {
return JSON.stringify(value);
Expand Down
Loading

0 comments on commit 11a8e3f

Please sign in to comment.