Skip to content

Commit 21d47f2

Browse files
committed
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
1 parent 8dbfd02 commit 21d47f2

File tree

8 files changed

+74
-67
lines changed

8 files changed

+74
-67
lines changed

lib/create-sinon-api.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"use strict";
2+
3+
const behavior = require("./sinon/behavior");
4+
const createSandbox = require("./sinon/create-sandbox");
5+
const extend = require("./sinon/util/core/extend");
6+
const fakeTimers = require("./sinon/util/fake-timers");
7+
const Sandbox = require("./sinon/sandbox");
8+
const stub = require("./sinon/stub");
9+
const promise = require("./sinon/promise");
10+
const assert = require("node:assert");
11+
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+
module.exports = function createApi({ sinonXhrLib }) {
18+
assert(sinonXhrLib, "No XHR lib passed in");
19+
20+
const apiMethods = {
21+
createSandbox: createSandbox,
22+
assert: require("./sinon/assert"),
23+
match: require("@sinonjs/samsam").createMatcher,
24+
restoreObject: require("./sinon/restore-object"),
25+
26+
expectation: require("./sinon/mock-expectation"),
27+
defaultConfig: require("./sinon/util/core/default-config"),
28+
29+
// fake timers
30+
timers: fakeTimers.timers,
31+
32+
// fake XHR
33+
xhr: sinonXhrLib.fakeXhr.xhr,
34+
FakeXMLHttpRequest: sinonXhrLib.fakeXhr.FakeXMLHttpRequest,
35+
36+
// fake server
37+
fakeServer: sinonXhrLib.fakeServer,
38+
fakeServerWithClock: sinonXhrLib.fakeServerWithClock,
39+
createFakeServer: sinonXhrLib.fakeServer.create.bind(
40+
sinonXhrLib.fakeServer,
41+
),
42+
createFakeServerWithClock: sinonXhrLib.fakeServerWithClock.create.bind(
43+
sinonXhrLib.fakeServerWithClock,
44+
),
45+
46+
addBehavior: function (name, fn) {
47+
behavior.addBehavior(stub, name, fn);
48+
},
49+
50+
// fake promise
51+
promise: promise,
52+
};
53+
54+
const sandbox = new Sandbox();
55+
return extend(sandbox, apiMethods);
56+
};

lib/sinon.js

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,6 @@
11
"use strict";
22

3-
const behavior = require("./sinon/behavior");
4-
const createSandbox = require("./sinon/create-sandbox");
5-
const extend = require("./sinon/util/core/extend");
6-
const fakeTimers = require("./sinon/util/fake-timers");
73
const nise = require("nise");
8-
const Sandbox = require("./sinon/sandbox");
9-
const stub = require("./sinon/stub");
10-
const promise = require("./sinon/promise");
4+
const createApi = require("./create-sinon-api");
115

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 });
57-
58-
module.exports = api;
59-
60-
// solely exposed for easier testing
61-
module.exports.createApi = createApi;
6+
module.exports = createApi({ sinonXhrLib: nise });

lib/sinon/color.js renamed to lib/sinon/colorizer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ module.exports = class Colorizer {
55
this.supportsColor = supportsColor;
66
}
77

8+
/**
9+
* Should be renamed to true #privateField
10+
* when we can ensure ES2022 support
11+
*
12+
* @private
13+
*/
814
colorize(str, color) {
915
if (this.supportsColor.stdout === false) {
1016
return str;

lib/sinon/spy-formatters.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22

33
const arrayProto = require("@sinonjs/commons").prototypes.array;
4-
const Colorizer = require("./color");
5-
const color = new Colorizer();
4+
const Colorizer = require("./colorizer");
5+
const colororizer = new Colorizer();
66
const match = require("@sinonjs/samsam").createMatcher;
77
const timesInWords = require("./util/core/times-in-words");
88
const inspect = require("util").inspect;
@@ -25,9 +25,9 @@ function colorSinonMatchText(matcher, calledArg, calledArgMessage) {
2525
let calledArgumentMessage = calledArgMessage;
2626
let matcherMessage = matcher.message;
2727
if (!matcher.test(calledArg)) {
28-
matcherMessage = color.red(matcher.message);
28+
matcherMessage = colororizer.red(matcher.message);
2929
if (calledArgumentMessage) {
30-
calledArgumentMessage = color.green(calledArgumentMessage);
30+
calledArgumentMessage = colororizer.green(calledArgumentMessage);
3131
}
3232
}
3333
return `${calledArgumentMessage} ${matcherMessage}`;
@@ -42,9 +42,9 @@ function colorDiffText(diff) {
4242
const objects = map(diff, function (part) {
4343
let text = part.value;
4444
if (part.added) {
45-
text = color.green(text);
45+
text = colororizer.green(text);
4646
} else if (part.removed) {
47-
text = color.red(text);
47+
text = colororizer.red(text);
4848
}
4949
if (diff.length === 2) {
5050
text += " "; // format simple diffs

test/assert-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const Colorizer = require("../lib/sinon/color");
3+
const Colorizer = require("../lib/sinon/colorizer");
44
const color = new Colorizer();
55
const referee = require("@sinonjs/referee");
66
const sinonStub = require("../lib/sinon/stub");

test/proxy-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = require("@sinonjs/referee").assert;
44
const extend = require("../lib/sinon/util/core/extend");
55
const createProxy = require("../lib/sinon/proxy");
66

7-
const Colorizer = require("../lib/sinon/color");
7+
const Colorizer = require("../lib/sinon/colorizer");
88
const color = new Colorizer();
99
const sinonSpy = require("../lib/sinon/spy");
1010
const sinonStub = require("../lib/sinon/stub");

test/sinon-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("sinon module", function () {
3131
useFakeXMLHttpRequest: "ba8bd609-c921-4a62-a1b9-49336bd426a4",
3232
},
3333
};
34-
sinon = require("../lib/sinon").createApi({
34+
sinon = require("../lib/create-sinon-api")({
3535
sinonXhrLib: fakeNise,
3636
});
3737
});

test/util/core/color-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
const assert = require("@sinonjs/referee").assert;
4-
const Colorizer = require("../../../lib/sinon/color");
4+
const Colorizer = require("../../../lib/sinon/colorizer");
55

66
const colors = [
77
{ name: "bold", code: 1 },

0 commit comments

Comments
 (0)