During test runs, we put every console message (stdout of swarm-cli) to an array. To assert correct output, we check these messages by exact index to match a regex or contain some substring.
This is often annoying and causes off-by-one errors when an extra step is added that shifts the indices.
I propose to change this approach with a less strict helper function linesMatchOrdered which takes an array of expected substrings (or regexes) to appear in the output, respecting the order, but ignoring gaps and omitting the indices.
function linesMatchOrdered(lines, expectedLines) {
let lineIndex = 0
for (let i = 0; i < expectedLines.length; i++) {
const expectedLine = expectedLines[i]
let found = false
while (!found && lineIndex < lines.length) {
if (lines[lineIndex].includes(expectedLine)) {
found = true
}
lineIndex++
}
if (!found) {
return false
}
}
return true
}
During test runs, we put every console message (stdout of swarm-cli) to an array. To assert correct output, we check these messages by exact index to match a regex or contain some substring.
This is often annoying and causes off-by-one errors when an extra step is added that shifts the indices.
I propose to change this approach with a less strict helper function
linesMatchOrdered
which takes an array of expected substrings (or regexes) to appear in the output, respecting the order, but ignoring gaps and omitting the indices.Before
After
Naive implementation