[ospec] Make the default reporting nicer looking (#2147)
* feat(ospec): Add spacing before report results ...sometimes tested code emits console.log() messages which then blend in with ospec's report * feat(ospec): Reword and prettify the report messages * feat(ospec): Make console.errors more compact, yet more readable * docs(ospec): Update change-log.md * fix(ospec): Fix grammar when number of assertions is 1 * feat(ospec): Make "all passed" message bright green * refactor(ospec): define `cStyle()` helper for browser styling * feat(ospec): Use en-dashes for the horizontal divider * feat(ospec): Revert stacktrace coloring, make context bright red ...and add extra newline above each error - for readability in commandline (node.js) mode * feat(ospec): Improve the only-test-passed message "1 assertion passed" --> "The 1 assertion passed" * docs: Update LOC count
This commit is contained in:
parent
11940b427b
commit
2794ceb76f
3 changed files with 32 additions and 10 deletions
|
|
@ -7,7 +7,7 @@ Noiseless testing framework
|
|||
|
||||
## About
|
||||
|
||||
- ~330 LOC including the CLI runner
|
||||
- ~360 LOC including the CLI runner
|
||||
- terser and faster test code than with mocha, jasmine or tape
|
||||
- test code reads like bullet points
|
||||
- assertion code follows [SVO](https://en.wikipedia.org/wiki/Subject–verb–object) structure in present tense for terseness and readability
|
||||
|
|
|
|||
|
|
@ -2,9 +2,8 @@
|
|||
|
||||
|
||||
## Upcoming...
|
||||
_2018-xx-xx_
|
||||
<!-- Add new lines here. Version number will be decided later -->
|
||||
- ...
|
||||
- Improved wording, spacing and color-coding of report messages and errors ([#2147](https://github.com/MithrilJS/mithril.js/pull/2147), [@maranomynet](https://github.com/maranomynet))
|
||||
|
||||
|
||||
## 2.0.0
|
||||
|
|
|
|||
|
|
@ -231,7 +231,7 @@ else window.o = m()
|
|||
function define(name, verb, compare) {
|
||||
Assert.prototype[name] = function assert(value) {
|
||||
if (compare(this.value, value)) record(null)
|
||||
else record(serialize(this.value) + "\n" + verb + "\n" + serialize(value))
|
||||
else record(serialize(this.value) + "\n " + verb + "\n" + serialize(value))
|
||||
return function(message) {
|
||||
var result = results[results.length - 1]
|
||||
result.message = message + "\n\n" + result.message
|
||||
|
|
@ -258,8 +258,17 @@ else window.o = m()
|
|||
else if (typeof value === "function") return value.name || "<anonymous function>"
|
||||
try {return JSON.stringify(value)} catch (e) {return String(value)}
|
||||
}
|
||||
function highlight(message) {
|
||||
return hasProcess ? (process.stdout.isTTY ? "\x1b[31m" + message + "\x1b[0m" : message) : "%c" + message + "%c "
|
||||
var colorCodes = {
|
||||
red: "31m",
|
||||
red2: "31;1m",
|
||||
green: "32;1m"
|
||||
}
|
||||
function highlight(message, color) {
|
||||
var code = colorCodes[color] || colorCodes.red;
|
||||
return hasProcess ? (process.stdout.isTTY ? "\x1b[" + code + message + "\x1b[0m" : message) : "%c" + message + "%c "
|
||||
}
|
||||
function cStyle(color, bold) {
|
||||
return hasProcess||!color ? "" : "color:"+color+(bold ? ";font-weight:bold" : "")
|
||||
}
|
||||
|
||||
o.report = function (results) {
|
||||
|
|
@ -267,14 +276,28 @@ else window.o = m()
|
|||
for (var i = 0, r; r = results[i]; i++) {
|
||||
if (!r.pass) {
|
||||
var stackTrace = o.cleanStackTrace(r.error)
|
||||
console.error(r.context + ":\n" + highlight(r.message) + (stackTrace ? "\n\n" + stackTrace + "\n\n" : ""), hasProcess ? "" : "color:red", hasProcess ? "" : "color:black")
|
||||
console.error(
|
||||
(hasProcess ? "\n" : "") +
|
||||
highlight(r.context + ":", "red2") + "\n" +
|
||||
highlight(r.message, "red") +
|
||||
(stackTrace ? "\n" + stackTrace + "\n" : ""),
|
||||
|
||||
cStyle("black", true), "", // reset to default
|
||||
cStyle("red"), cStyle("black")
|
||||
)
|
||||
errCount++
|
||||
}
|
||||
}
|
||||
var pl = results.length === 1 ? "" : "s"
|
||||
var resultSummary = (errCount === 0) ?
|
||||
highlight((pl ? "All " : "The ") + results.length + " assertion" + pl + " passed", "green"):
|
||||
highlight(errCount + " out of " + results.length + " assertion" + pl + " failed", "red2")
|
||||
var runningTime = " in " + Math.round(Date.now() - start) + "ms"
|
||||
|
||||
console.log(
|
||||
(name ? name + ": " : "") +
|
||||
results.length + " assertions completed in " + Math.round(new Date - start) + "ms, " +
|
||||
"of which " + results.filter(function(result){return result.error}).length + " failed"
|
||||
(hasProcess ? "––––––\n" : "") +
|
||||
(name ? name + ": " : "") + resultSummary + runningTime,
|
||||
cStyle((errCount === 0 ? "green" : "red"), true), ""
|
||||
)
|
||||
return errCount
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue