Skip to content

Commit 11a8e3f

Browse files
committed
Remove Proxyquire as it did not handle newer syntax
The defaults of Prettier 3 threw Proxyquirify into a frenzy. Did the minimal rewrite (DI) that worked
1 parent ec37d63 commit 11a8e3f

File tree

9 files changed

+101
-667
lines changed

9 files changed

+101
-667
lines changed

lib/sinon.js

+48-35
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,53 @@ const Sandbox = require("./sinon/sandbox");
99
const stub = require("./sinon/stub");
1010
const promise = require("./sinon/promise");
1111

12-
const apiMethods = {
13-
createSandbox: createSandbox,
14-
assert: require("./sinon/assert"),
15-
match: require("@sinonjs/samsam").createMatcher,
16-
restoreObject: require("./sinon/restore-object"),
17-
18-
expectation: require("./sinon/mock-expectation"),
19-
defaultConfig: require("./sinon/util/core/default-config"),
20-
21-
// fake timers
22-
timers: fakeTimers.timers,
23-
24-
// fake XHR
25-
xhr: nise.fakeXhr.xhr,
26-
FakeXMLHttpRequest: nise.fakeXhr.FakeXMLHttpRequest,
27-
28-
// fake server
29-
fakeServer: nise.fakeServer,
30-
fakeServerWithClock: nise.fakeServerWithClock,
31-
createFakeServer: nise.fakeServer.create.bind(nise.fakeServer),
32-
createFakeServerWithClock: nise.fakeServerWithClock.create.bind(
33-
nise.fakeServerWithClock,
34-
),
35-
36-
addBehavior: function (name, fn) {
37-
behavior.addBehavior(stub, name, fn);
38-
},
39-
40-
// fake promise
41-
promise: promise,
42-
};
43-
44-
const sandbox = new Sandbox();
45-
46-
const api = extend(sandbox, apiMethods);
12+
/**
13+
* @param {object} opts injection point to override the default XHR lib in testing
14+
* @param {object} opts.sinonXhrLib
15+
* @returns {object} a configured sandbox
16+
*/
17+
function createApi({ sinonXhrLib }) {
18+
const apiMethods = {
19+
createSandbox: createSandbox,
20+
assert: require("./sinon/assert"),
21+
match: require("@sinonjs/samsam").createMatcher,
22+
restoreObject: require("./sinon/restore-object"),
23+
24+
expectation: require("./sinon/mock-expectation"),
25+
defaultConfig: require("./sinon/util/core/default-config"),
26+
27+
// fake timers
28+
timers: fakeTimers.timers,
29+
30+
// fake XHR
31+
xhr: sinonXhrLib.fakeXhr.xhr,
32+
FakeXMLHttpRequest: sinonXhrLib.fakeXhr.FakeXMLHttpRequest,
33+
34+
// fake server
35+
fakeServer: sinonXhrLib.fakeServer,
36+
fakeServerWithClock: sinonXhrLib.fakeServerWithClock,
37+
createFakeServer: sinonXhrLib.fakeServer.create.bind(
38+
sinonXhrLib.fakeServer,
39+
),
40+
createFakeServerWithClock: sinonXhrLib.fakeServerWithClock.create.bind(
41+
sinonXhrLib.fakeServerWithClock,
42+
),
43+
44+
addBehavior: function (name, fn) {
45+
behavior.addBehavior(stub, name, fn);
46+
},
47+
48+
// fake promise
49+
promise: promise,
50+
};
51+
52+
const sandbox = new Sandbox();
53+
return extend(sandbox, apiMethods);
54+
}
55+
56+
const api = createApi({ sinonXhrLib: nise });
4757

4858
module.exports = api;
59+
60+
// solely exposed for easier testing
61+
module.exports.createApi = createApi;

lib/sinon/color.js

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
"use strict";
22

3-
const supportsColor = require("supports-color");
4-
5-
function colorize(str, color) {
6-
if (supportsColor.stdout === false) {
7-
return str;
3+
module.exports = class Colorizer {
4+
constructor(supportsColor = require("supports-color")) {
5+
this.supportsColor = supportsColor;
86
}
97

10-
return `\x1b[${color}m${str}\x1b[0m`;
11-
}
8+
colorize(str, color) {
9+
if (this.supportsColor.stdout === false) {
10+
return str;
11+
}
1212

13-
exports.red = function (str) {
14-
return colorize(str, 31);
15-
};
13+
return `\x1b[${color}m${str}\x1b[0m`;
14+
}
1615

17-
exports.green = function (str) {
18-
return colorize(str, 32);
19-
};
16+
red(str) {
17+
return this.colorize(str, 31);
18+
}
2019

21-
exports.cyan = function (str) {
22-
return colorize(str, 96);
23-
};
20+
green(str) {
21+
return this.colorize(str, 32);
22+
}
2423

25-
exports.white = function (str) {
26-
return colorize(str, 39);
27-
};
24+
cyan(str) {
25+
return this.colorize(str, 96);
26+
}
2827

29-
exports.bold = function (str) {
30-
return colorize(str, 1);
28+
white(str) {
29+
return this.colorize(str, 39);
30+
}
31+
32+
bold(str) {
33+
return this.colorize(str, 1);
34+
}
3135
};

lib/sinon/spy-formatters.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22

33
const arrayProto = require("@sinonjs/commons").prototypes.array;
4-
const color = require("./color");
4+
const Colorizer = require("./color");
5+
const color = new Colorizer();
56
const match = require("@sinonjs/samsam").createMatcher;
67
const timesInWords = require("./util/core/times-in-words");
78
const inspect = require("util").inspect;
@@ -12,6 +13,12 @@ const map = arrayProto.map;
1213
const push = arrayProto.push;
1314
const slice = arrayProto.slice;
1415

16+
/**
17+
*
18+
* @param matcher
19+
* @param calledArg
20+
* @param calledArgMessage
21+
*/
1522
function colorSinonMatchText(matcher, calledArg, calledArgMessage) {
1623
let calledArgumentMessage = calledArgMessage;
1724
let matcherMessage = matcher.message;
@@ -24,6 +31,10 @@ function colorSinonMatchText(matcher, calledArg, calledArgMessage) {
2431
return `${calledArgumentMessage} ${matcherMessage}`;
2532
}
2633

34+
/**
35+
*
36+
* @param diff
37+
*/
2738
function colorDiffText(diff) {
2839
const objects = map(diff, function (part) {
2940
let text = part.value;
@@ -40,6 +51,10 @@ function colorDiffText(diff) {
4051
return join(objects, "");
4152
}
4253

54+
/**
55+
*
56+
* @param value
57+
*/
4358
function quoteStringValue(value) {
4459
if (typeof value === "string") {
4560
return JSON.stringify(value);

0 commit comments

Comments
 (0)