Skip to content

Commit

Permalink
test: Show diff in colors
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jun 17, 2024
1 parent 4d926ee commit b819fc2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
35 changes: 28 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
yellow: "\x1b[33m",
reset: "\x1b[0m",
color: (_process.stdout || /* c8 ignore next */ _process).isTTY,
cut: 1500,
cut: 15000,
delay: 1,
seed: (Math.random() * 1e5)|0,
stack: 9,
Expand Down Expand Up @@ -300,10 +300,18 @@
if (value) {
testCase.passed++
} else {
fail("Assertion:" + testCase.total + ": " + (message ?
message + "\nexpected: " + stringify(expected) + "\nactual: " + stringify(actual) :
stringify(value) + " is truthy"
))
if (message) {
expected = "expected: " + stringify(expected)
actual = "actual: " + stringify(actual)
if (conf.color && expected.length > 80) {
message += "\n" + diff(expected, actual, ["\n", " ", ","], conf.red, conf.reset, conf.green, conf.reset)
} else {
message += "\n" + expected + "\n" + actual
}
} else {
message = stringify(value) + " is truthy"
}
fail("Assertion:" + testCase.total + ": " + message)
}
return testCase.plan(testCase.planned)
}
Expand Down Expand Up @@ -399,8 +407,12 @@
function This() {
return this
}
function diff(a, b, sep) {
function diff(a, b, sep, r1, r2, g1, g2) {
sep = sep || ""
if (_isArray(sep)) {
for (del = 0; (ins = sep[del++]) && (pre = a.split(ins)).length < 3; );
sep = ins || ""
}
a = a.split(sep)
b = b.split(sep)
var del, ins, pre
Expand All @@ -426,10 +438,19 @@
del = 0
ins = pre
}
out.push([aPos - del, del, b.slice(bPos - ins, bPos).join(sep)]);
out.push([aPos - del, del, b.slice(bPos - ins, bPos).join(sep)])
}
}
}
if (r1) {
for (bPos = 0; (b = out[bPos++]); ) {
a.splice(b[0], b[1],
(b[1] ? r1 + a.slice(b[0], b[0] + b[1]).join(sep) + r2 : "") +
(b[2] ? g1 + b[2] + g2 : "")
)
}
out = a.join(sep)
}
return out
}
function format(str, map, fallback) {
Expand Down
5 changes: 2 additions & 3 deletions test/spec/test-first.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
---
✘ 1. it should fail [0/1]
Error: Assertion:1: equal
expected: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
actual: ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]
expected:actual:  "aaaaaaa [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]
at Function.<anonymous> (test/test-fail.js:7:9)
at nextCase (test.js:288:19)
at nextSuite (test.js:363:3)
at nextSuite (test.js:371:3)
at Timeout.nextCase [as _onTimeout] (test.js:259:26)
...
# FAIL tests 1
Expand Down

0 comments on commit b819fc2

Please sign in to comment.