Expose the reporter as o.report(results), have it return the number of errors

This commit is contained in:
Pierre-Yves Gérardy 2017-11-30 23:25:31 +01:00
parent 3164031ba4
commit ff330e5605

View file

@ -4,7 +4,7 @@
if (typeof module !== "undefined") module["exports"] = m() if (typeof module !== "undefined") module["exports"] = m()
else window.o = m() else window.o = m()
})(function init(name) { })(function init(name) {
var spec = {}, subjects = [], results, only = null, ctx = spec, start, stack = 0, nextTickish, hasProcess = typeof process === "object", reporter, hasOwn = ({}).hasOwnProperty var spec = {}, subjects = [], results, only = null, ctx = spec, start, stack = 0, nextTickish, hasProcess = typeof process === "object", hasOwn = ({}).hasOwnProperty
if (name != null) spec[name] = ctx = {} if (name != null) spec[name] = ctx = {}
@ -71,11 +71,18 @@ else window.o = m()
// now we're in user code // now we're in user code
return stack[i] return stack[i]
} }
o.run = function(_reporter) { o.run = function(reporter) {
results = [] results = []
start = new Date start = new Date
reporter = _reporter test(spec, [], [], function() {
test(spec, [], [], function() {setTimeout(report)}) setTimeout(function () {
if (typeof reporter === "function") reporter(results)
else {
var errCount = o.report(results)
if (hasProcess && errCount !== 0) process.exit(1)
}
})
})
function test(spec, pre, post, finalize) { function test(spec, pre, post, finalize) {
pre = [].concat(pre, spec["__beforeEach"] || []) pre = [].concat(pre, spec["__beforeEach"] || [])
@ -255,16 +262,13 @@ else window.o = m()
return hasProcess ? "\x1b[31m" + message + "\x1b[0m" : "%c" + message + "%c " return hasProcess ? "\x1b[31m" + message + "\x1b[0m" : "%c" + message + "%c "
} }
function report() { o.report = function (results) {
var status = 0 var errCount = 0
if (typeof reporter === "function") return reporter(results)
for (var i = 0, r; r = results[i]; i++) { for (var i = 0, r; r = results[i]; i++) {
if (!r.pass) { if (!r.pass) {
var stackTrace = o.cleanStackTrace(r.error) 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(r.context + ":\n" + highlight(r.message) + (stackTrace ? "\n\n" + stackTrace + "\n\n" : ""), hasProcess ? "" : "color:red", hasProcess ? "" : "color:black")
status = 1 errCount++
} }
} }
console.log( console.log(
@ -272,10 +276,10 @@ else window.o = m()
results.length + " assertions completed in " + Math.round(new Date - start) + "ms, " + results.length + " assertions completed in " + Math.round(new Date - start) + "ms, " +
"of which " + results.filter(function(result){return result.error}).length + " failed" "of which " + results.filter(function(result){return result.error}).length + " failed"
) )
if (hasProcess && status === 1) process.exit(1) return errCount
} }
if(hasProcess) { if (hasProcess) {
nextTickish = process.nextTick nextTickish = process.nextTick
} else { } else {
nextTickish = function fakeFastNextTick(next) { nextTickish = function fakeFastNextTick(next) {