Skip to content

Commit 6b3dac6

Browse files
committed
Use util.inspect for formatting
As part of a larger effort, we are retiring `@sinonjs/formatio`. When it was created Node was quite immature and there were no other community projects for solid object formatting. Ten years later, Node is mature and has great support for formatting all sorts of values. Removing the use of `@sinonjs/formatio` in favour of Node's `util.inspect` removes an entire project's worth of maintenance burden from the organisation. In practial terms, it means that some output from `sinon` will look slightly different. This won't cause a new major release as this output is only intended to be read by humans and not be part of an API promise.
1 parent 2608a71 commit 6b3dac6

6 files changed

Lines changed: 99 additions & 81 deletions

File tree

lib/sinon/util/core/format.js

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

3-
var formatio = require("@sinonjs/formatio");
4-
5-
var formatter = formatio.configure({
6-
quoteStrings: false,
7-
limitChildrenCount: 250
8-
});
9-
3+
var inspect = require("util").inspect;
104
var customFormatter;
115

126
function format() {
137
if (customFormatter) {
148
return customFormatter.apply(null, arguments);
159
}
1610

17-
return formatter.ascii.apply(formatter, arguments);
11+
return inspect.apply(inspect, arguments);
1812
}
1913

2014
format.setFormatter = function(aCustomFormatter) {

test/assert-test.js

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var sinonAssert = require("../lib/sinon/assert");
88
var match = require("@sinonjs/samsam").createMatcher;
99
var assert = referee.assert;
1010
var refute = referee.refute;
11+
var inspect = require("util").inspect;
1112

1213
function requiresValidFake(method) {
1314
it("should fail with non-function fake", function() {
@@ -1596,7 +1597,12 @@ describe("assert", function() {
15961597

15971598
assert.equals(
15981599
this.message("calledOn", this.obj.doSomething, this.obj),
1599-
"expected doSomething to be called with [Oh yeah] as this but was called with [Oh no], [Oh well]"
1600+
"expected doSomething to be called with " +
1601+
inspect(this.obj) +
1602+
" as this but was called with " +
1603+
inspect(obj) +
1604+
", " +
1605+
inspect(obj2)
16001606
);
16011607
});
16021608

@@ -1622,8 +1628,14 @@ describe("assert", function() {
16221628

16231629
assert.equals(
16241630
this.message("alwaysCalledOn", this.obj.doSomething, this.obj),
1625-
"expected doSomething to always be called with [Oh yeah] as this but was called with " +
1626-
"[Oh no], [Oh well], [Oh yeah]"
1631+
"expected doSomething to always be called with " +
1632+
inspect(this.obj) +
1633+
" as this but was called with " +
1634+
inspect(obj) +
1635+
", " +
1636+
inspect(obj2) +
1637+
", " +
1638+
inspect(this.obj)
16271639
);
16281640
});
16291641

@@ -1657,7 +1669,7 @@ describe("assert", function() {
16571669
color.green("1") +
16581670
" \n" +
16591671
"3\n" +
1660-
'"hey"'
1672+
inspect('"hey"')
16611673
);
16621674
});
16631675

@@ -1674,13 +1686,14 @@ describe("assert", function() {
16741686
color.green("1") +
16751687
" \n" +
16761688
"3\n" +
1677-
'"hey"\n' +
1689+
inspect('"hey"') +
1690+
"\n" +
16781691
"Call 2:\n" +
16791692
"1\n" +
16801693
"3\n" +
1681-
color.red('"not"') +
1694+
color.red(inspect('"not"')) +
16821695
" " +
1683-
color.green('"hey"') +
1696+
color.green(inspect('"hey"')) +
16841697
" "
16851698
);
16861699
});
@@ -1706,18 +1719,45 @@ describe("assert", function() {
17061719
},
17071720
"fifth"
17081721
];
1709-
assert.equals(
1710-
this.message("calledWith", this.obj.doSomething, expectedArg).replace(/ at.*/g, ""),
1711-
"expected doSomething to be called with arguments \n" +
1712-
"[{\n" +
1713-
' first: "a",\n' +
1714-
color.red(" mismatchKey: true,\n") +
1715-
color.green(" mismatchKeyX: true,\n") +
1716-
" second: { nest: true },\n" +
1717-
color.red(" third: [{ fourth: { nest: true } }]\n") +
1718-
color.green(" third: [{ fourth: { nest: false } }]\n") +
1719-
'}, "fifth"]'
1720-
);
1722+
1723+
var actual = this.message("calledWith", this.obj.doSomething, expectedArg);
1724+
1725+
/**
1726+
* Unfortunately, `util.inspect` behaves differently in node than
1727+
* it does in browsers, so we need to detect the difference in order
1728+
* to set the correct value for expected `expected`.
1729+
*
1730+
* In node the output uses more whitespace than in browsers.
1731+
*
1732+
* @type {Boolean}
1733+
*/
1734+
var usesCondensedFormat =
1735+
inspect([
1736+
{ apple: "e4d13f88-9b9b-4e05-8abb-f76df2d4ef40" },
1737+
{ pear: "841b661f-80f4-4560-9cf4-133dcffd240c" }
1738+
]).indexOf("[ {") === 0;
1739+
1740+
var expected = usesCondensedFormat
1741+
? "expected doSomething to be called with arguments \n" +
1742+
"[ { first: 'a',\n" +
1743+
" second: { nest: true },\n" +
1744+
" third: [ [Object] ],\n" +
1745+
color.red(" mismatchKey: true },\n") +
1746+
color.green(" mismatchKeyX: true },\n") +
1747+
" 'fifth' ]"
1748+
: "expected doSomething to be called with arguments \n" +
1749+
"[\n" +
1750+
" {\n" +
1751+
" first: 'a',\n" +
1752+
" second: { nest: true },\n" +
1753+
" third: [ [Object] ],\n" +
1754+
color.red(" mismatchKey: true\n") +
1755+
color.green(" mismatchKeyX: true\n") +
1756+
" },\n" +
1757+
" 'fifth'\n" +
1758+
"]";
1759+
1760+
assert.equals(actual, expected);
17211761
});
17221762

17231763
it("assert.calledWith exception message with a missing argument", function() {
@@ -1892,7 +1932,7 @@ describe("assert", function() {
18921932
color.green("4") +
18931933
" \n" +
18941934
"3\n" +
1895-
'"hey"'
1935+
inspect('"hey"')
18961936
);
18971937
});
18981938

@@ -1907,13 +1947,13 @@ describe("assert", function() {
19071947
"1\n" +
19081948
color.red("3") +
19091949
" " +
1910-
color.green('"hey"') +
1950+
color.green(inspect('"hey"')) +
19111951
" \n" +
1912-
color.red('"hey"') +
1952+
color.red(inspect('"hey"')) +
19131953
"\n" +
19141954
"Call 2:\n" +
19151955
"1\n" +
1916-
'"hey"'
1956+
inspect('"hey"')
19171957
);
19181958
});
19191959

@@ -1928,13 +1968,13 @@ describe("assert", function() {
19281968
"1\n" +
19291969
color.red("3") +
19301970
" " +
1931-
color.green('"hey"') +
1971+
color.green(inspect('"hey"')) +
19321972
" \n" +
1933-
color.red('"hey"') +
1973+
color.red(inspect('"hey"')) +
19341974
"\n" +
19351975
"Call 2:\n" +
19361976
"1\n" +
1937-
'"hey"'
1977+
inspect('"hey"')
19381978
);
19391979
});
19401980

@@ -1943,7 +1983,7 @@ describe("assert", function() {
19431983

19441984
assert.equals(
19451985
this.message("calledWithExactly", this.obj.doSomething, 1, 3).replace(/ at.*/g, ""),
1946-
"expected doSomething to be called with exact arguments \n1\n3\n" + color.red('"hey"')
1986+
"expected doSomething to be called with exact arguments \n1\n3\n" + color.red(inspect('"hey"'))
19471987
);
19481988
});
19491989

@@ -1962,7 +2002,7 @@ describe("assert", function() {
19622002
color.green("1") +
19632003
" \n" +
19642004
"3\n" +
1965-
'"bob"'
2005+
inspect('"bob"')
19662006
);
19672007

19682008
this.obj.doSomething();
@@ -1974,7 +2014,7 @@ describe("assert", function() {
19742014
"\n" +
19752015
color.red("3") +
19762016
"\n" +
1977-
color.red(JSON.stringify('"bob"')) +
2017+
color.red(inspect(JSON.stringify('"bob"'))) +
19782018
"\n" +
19792019
"Call 2:"
19802020
);
@@ -1988,7 +2028,7 @@ describe("assert", function() {
19882028
"expected doSomething to be called with arguments \n" +
19892029
color.red(1234) +
19902030
" " +
1991-
color.green('"1234"') +
2031+
color.green(inspect('"1234"')) +
19922032
" "
19932033
);
19942034
});
@@ -2003,7 +2043,7 @@ describe("assert", function() {
20032043
"Call 1:\n" +
20042044
"1\n" +
20052045
"3\n" +
2006-
color.red('"hey"') +
2046+
color.red(inspect('"hey"')) +
20072047
"\n" +
20082048
"Call 2:\n" +
20092049
"1\n" +
@@ -2035,7 +2075,7 @@ describe("assert", function() {
20352075

20362076
assert.equals(
20372077
this.message("threw", this.obj.doSomething).replace(/ at.*/g, ""),
2038-
"doSomething did not throw exception\n doSomething(1, 3, hey)\n doSomething(1, 3)"
2078+
"doSomething did not throw exception\n doSomething(1, 3, 'hey')\n doSomething(1, 3)"
20392079
);
20402080
});
20412081

@@ -2045,14 +2085,14 @@ describe("assert", function() {
20452085

20462086
assert.equals(
20472087
this.message("alwaysThrew", this.obj.doSomething).replace(/ at.*/g, ""),
2048-
"doSomething did not always throw exception\n doSomething(1, 3, hey)\n doSomething(1, 3)"
2088+
"doSomething did not always throw exception\n doSomething(1, 3, 'hey')\n doSomething(1, 3)"
20492089
);
20502090
});
20512091

20522092
it("assert.match exception message", function() {
20532093
assert.equals(
20542094
this.message("match", { foo: 1 }, [1, 3]),
2055-
"expected value to match\n expected = [1, 3]\n actual = { foo: 1 }"
2095+
"expected value to match\n expected = [ 1, 3 ]\n actual = { foo: 1 }"
20562096
);
20572097
});
20582098
});

test/mock-test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ describe("sinonMock", function() {
822822
expectation.verify();
823823
},
824824
{
825-
message: "Expected myMeth([...]) once (never called)"
825+
message: "Expected myMeth('[...]') once (never called)"
826826
}
827827
);
828828
});
@@ -884,8 +884,8 @@ describe("sinonMock", function() {
884884
},
885885
{
886886
message:
887-
"Expected method([...]) thrice (never called)\n" +
888-
"Expected method(42[, ...]) once (never called)"
887+
"Expected method('[...]') thrice (never called)\n" +
888+
"Expected method(42, '[...]') once (never called)"
889889
}
890890
);
891891
});
@@ -960,7 +960,7 @@ describe("sinonMock", function() {
960960
{
961961
message:
962962
"Unexpected call: method()\n" +
963-
" Expectation met: method(1[, ...]) once\n" +
963+
" Expectation met: method(1, '[...]') once\n" +
964964
" Expected method(42) thrice (never called)"
965965
}
966966
);
@@ -982,7 +982,7 @@ describe("sinonMock", function() {
982982
mock.verify();
983983
},
984984
{
985-
message: "Expected method(42) thrice (never called)\nExpectation met: method(1[, ...]) once"
985+
message: "Expected method(42) thrice (never called)\nExpectation met: method(1, '[...]') once"
986986
}
987987
);
988988
});
@@ -996,7 +996,7 @@ describe("sinonMock", function() {
996996
mock.verify();
997997
},
998998
{
999-
message: "Expected method([...]) at least once (never called)"
999+
message: "Expected method('[...]') at least once (never called)"
10001000
}
10011001
);
10021002
});
@@ -1014,7 +1014,7 @@ describe("sinonMock", function() {
10141014
object.method();
10151015
},
10161016
{
1017-
message: "Unexpected call: method()\n Expectation met: method([...]) at most twice"
1017+
message: "Unexpected call: method()\n Expectation met: method('[...]') at most twice"
10181018
}
10191019
);
10201020
});
@@ -1037,8 +1037,8 @@ describe("sinonMock", function() {
10371037
{
10381038
message:
10391039
"Unexpected call: method(2)\n" +
1040-
" Expectation met: method([...]) at least once\n" +
1041-
" Expectation met: method(2[, ...]) once"
1040+
" Expectation met: method('[...]') at least once\n" +
1041+
" Expectation met: method(2, '[...]') once"
10421042
}
10431043
);
10441044
});
@@ -1054,7 +1054,7 @@ describe("sinonMock", function() {
10541054
mock.verify();
10551055
},
10561056
{
1057-
message: "Expected method([...]) at least once and at most twice (never called)"
1057+
message: "Expected method('[...]') at least once and at most twice (never called)"
10581058
}
10591059
);
10601060
});

test/proxy-call-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ describe("sinonSpy.call", function() {
10791079
.getCall(0)
10801080
.toString()
10811081
.replace(/ at.*/g, ""),
1082-
"doIt(42, Hey)"
1082+
"doIt(42, 'Hey')"
10831083
);
10841084
});
10851085

@@ -1092,7 +1092,7 @@ describe("sinonSpy.call", function() {
10921092
.getCall(0)
10931093
.toString()
10941094
.replace(/ at.*/g, ""),
1095-
"doIt(42, Hey) => 42"
1095+
"doIt(42, 'Hey') => 42"
10961096
);
10971097
});
10981098

@@ -1105,7 +1105,7 @@ describe("sinonSpy.call", function() {
11051105
.getCall(0)
11061106
.toString()
11071107
.replace(/ at.*/g, ""),
1108-
"doIt(42, Hey) => (empty string)"
1108+
"doIt(42, 'Hey') => ''"
11091109
);
11101110
});
11111111

0 commit comments

Comments
 (0)