Skip to content

Instantly share code, notes, and snippets.

@Bogidon
Last active February 8, 2017 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bogidon/d088c94596d910dc755660365fda4d97 to your computer and use it in GitHub Desktop.
Save Bogidon/d088c94596d910dc755660365fda4d97 to your computer and use it in GitHub Desktop.
The default Mocha "spec" reporter, but with colors formatted for browser consoles. Original reporter: https://github.com/mochajs/mocha/blob/master/lib/reporters/spec.js
'use strict'
let mocha = require('mocha')
let Base = mocha.reporters.Base
exports = module.exports = BrowserSpec
// This class is a ported version of Mocha's default "spec" reporter to format color output for the browser.
// https://github.com/mochajs/mocha/blob/master/lib/reporters/spec.js
function BrowserSpec (runner) {
mocha.reporters.Base.call(this, runner)
const greenColor = 'color:green;'
const redColor = 'color:red;'
const grayColor = 'color:darkGray;'
const cyanColor = 'color:cyan;'
const blackColor = 'color:black;'
const yellowColor = 'color:orange;'
let passes = 0
let failures = 0
let indents = 0
let n = 0
function indent () {
return Array(indents).join(' ')
}
runner.on('start', function () {
console.log()
})
runner.on('suite', function (suite) {
++indents
console.log('%c%s%s', grayColor, indent(), suite.title)
})
runner.on('suite end', function () {
--indents
if (indents === 1) {
console.log()
}
})
runner.on('pending', function (test) {
let fmt = indent() + '%c - %s'
console.log(fmt, cyanColor, test.title)
})
runner.on('pass', function (test) {
passes++
let fmt
if (test.speed === 'fast') {
fmt = indent() +
'%c ' + Base.symbols.ok + '%c %s'
console.log(fmt, greenColor, blackColor, test.title)
} else {
let color = (test === 'slow') ? redColor : yellowColor
fmt = indent() +
'%c ' + Base.symbols.ok +
'%c %s' +
'%c (%dms)'
console.log(fmt, greenColor, blackColor, test.title, color, test.duration)
}
})
runner.on('fail', function (test) {
failures++
console.log(indent() + '%c %d) %s', redColor, ++n, test.title)
})
runner.on('end', function () {
console.log('All done!')
console.log('%c%d passing', greenColor, passes)
console.log('%c%d failing', redColor, failures)
process.exit()
})
}
(The MIT License)
Copyright (c) 2017 Bogdan Vitoc
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment