Skip to content

Commit dff6643

Browse files
committed
update: util for joining strings in test
1 parent 6124218 commit dff6643

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

__tests__/to-log.test.js

+30-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require("../src/index");
22

33
const { describe, expect, it } = require("@jest/globals");
4-
const { LINE_TERMINATOR } = require("../src/utils");
4+
const { LINE_TERMINATOR, joinRight } = require("../src/utils");
55

66
describe("extended matchers", () => {
77
describe("toLog", () => {
@@ -25,13 +25,13 @@ describe("extended matchers", () => {
2525
console.log({ 1: 1, 2: [3, 4, 5] }, [100, 200, 300], undefined, null);
2626
}
2727

28-
const expectedString =
29-
"{} []" +
30-
LINE_TERMINATOR +
31-
"[] {}" +
32-
LINE_TERMINATOR +
33-
"{ '1': 1, '2': [ 3, 4, 5 ] } [ 100, 200, 300 ] undefined null" +
34-
LINE_TERMINATOR;
28+
const expectedStringArr = [
29+
"{} []",
30+
"[] {}",
31+
"{ '1': 1, '2': [ 3, 4, 5 ] } [ 100, 200, 300 ] undefined null",
32+
];
33+
34+
const expectedString = joinRight(expectedStringArr, LINE_TERMINATOR);
3535
expect(testFn).toLog(expectedString);
3636
});
3737
});
@@ -60,14 +60,13 @@ describe("extended matchers", () => {
6060
);
6161
}
6262

63-
const expectedString =
64-
"Vello 300" +
65-
LINE_TERMINATOR +
66-
"Vello 400" +
67-
LINE_TERMINATOR +
68-
"{ '1': 1, '2': [ 3, 4, 5 ] } [ 100, 200, 300 ] undefined null" +
69-
LINE_TERMINATOR;
63+
const expectedStringArr = [
64+
"Vello 300",
65+
"Vello 400",
66+
"{ '1': 1, '2': [ 3, 4, 5 ] } [ 100, 200, 300 ] undefined null",
67+
];
7068

69+
const expectedString = joinRight(expectedStringArr, LINE_TERMINATOR);
7170
expect(testFn).toLog(expectedString);
7271
});
7372
});
@@ -95,14 +94,13 @@ describe("extended matchers", () => {
9594
);
9695
}
9796

98-
const expectedString =
99-
"Cello 300" +
100-
LINE_TERMINATOR +
101-
"Cello 400" +
102-
LINE_TERMINATOR +
103-
"{ '1': 1, '2': [ 3, 4, 5 ] } [ 100, 200, 300 ] undefined null" +
104-
LINE_TERMINATOR;
97+
const expectedStringArr = [
98+
"Cello 300",
99+
"Cello 400",
100+
"{ '1': 1, '2': [ 3, 4, 5 ] } [ 100, 200, 300 ] undefined null",
101+
];
105102

103+
const expectedString = joinRight(expectedStringArr, LINE_TERMINATOR);
106104
expect(testFn).toLog(expectedString);
107105
});
108106
});
@@ -177,17 +175,14 @@ describe("extended matchers", () => {
177175
console.error("Gello 123");
178176
}
179177

180-
const expected =
181-
"Jello warning" +
182-
LINE_TERMINATOR +
183-
"Gello error" +
184-
LINE_TERMINATOR +
185-
"Jello 123" +
186-
LINE_TERMINATOR +
187-
"Gello 123" +
188-
LINE_TERMINATOR;
189-
190-
expect(testFn).toLogErrorOrWarn(expected);
178+
const expectedStringArr = [
179+
"Jello warning",
180+
"Gello error",
181+
"Jello 123",
182+
"Gello 123",
183+
];
184+
const expectedString = joinRight(expectedStringArr, LINE_TERMINATOR);
185+
expect(testFn).toLogErrorOrWarn(expectedString);
191186
});
192187

193188
it("should not honor other methods", () => {
@@ -241,11 +236,10 @@ describe("extended matchers", () => {
241236
}
242237

243238
await expect(asyncTestFn).toLog(
244-
["ASYNC log", "ASYNC info", "ASYNC debug"].join(LINE_TERMINATOR) +
245-
LINE_TERMINATOR
239+
joinRight(["ASYNC log", "ASYNC info", "ASYNC debug"], LINE_TERMINATOR)
246240
);
247241
await expect(asyncTestFn).toLogErrorOrWarn(
248-
["ASYNC warn", "ASYNC error"].join(LINE_TERMINATOR) + LINE_TERMINATOR
242+
joinRight(["ASYNC warn", "ASYNC error"], LINE_TERMINATOR)
249243
);
250244
});
251245

src/utils.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,28 @@ async function invokeFunction(fn) {
2727
}
2828
}
2929

30+
/**
31+
* Concatenates separator to the right of `Array.join(separator)`
32+
* @param {Array} arr
33+
* @param {string} [separator] - If not `string` or `undefined`, implicitly coerced to a string
34+
* @returns {string}
35+
*/
36+
function joinRight(arr, separator) {
37+
if (!Array.isArray(arr)) {
38+
throw new TypeError(
39+
`Must be of type Array; Received: ${getClassName(arr)}`
40+
);
41+
}
42+
43+
return arr.join(separator) + (separator === undefined ? "," : separator);
44+
}
45+
3046
module.exports = {
3147
EMPTY_STRING,
3248
LINE_TERMINATOR,
3349
checkLogMatchersArgs,
3450
getClassName,
3551
isAsyncFunction,
36-
invokeFunction
52+
invokeFunction,
53+
joinRight,
3754
};

0 commit comments

Comments
 (0)