Log longest specs/suites during grunt test

This commit is contained in:
Kevin Sawicki 2013-08-28 22:07:57 -07:00
parent 3e626f28c4
commit e40d837b40
2 changed files with 35 additions and 23 deletions

View File

@ -15,6 +15,7 @@ module.exports.runSpecSuite = (specSuite, logErrors=true) ->
$ = require 'jquery'
TimeReporter = require 'time-reporter'
timeReporter = new TimeReporter()
if atom.getLoadSettings().exitWhenDone
{jasmineNode} = require 'jasmine-node/lib/jasmine-node/reporter'
@ -22,6 +23,10 @@ module.exports.runSpecSuite = (specSuite, logErrors=true) ->
print: (args...) ->
process.stderr.write(args...)
onComplete: (runner) ->
process.stdout.write('\n')
timeReporter.logLongestSuites 10, (line) -> process.stdout.write("#{line}\n")
process.stdout.write('\n')
timeReporter.logLongestSpecs 10, (line) -> process.stdout.write("#{line}\n")
atom.exit(runner.results().failedCount > 0 ? 1 : 0)
else
AtomReporter = require 'atom-reporter'
@ -31,7 +36,7 @@ module.exports.runSpecSuite = (specSuite, logErrors=true) ->
jasmineEnv = jasmine.getEnv()
jasmineEnv.addReporter(reporter)
jasmineEnv.addReporter(new TimeReporter())
jasmineEnv.addReporter(timeReporter)
$('body').append $$ -> @div id: 'jasmine-content'

View File

@ -1,52 +1,59 @@
_ = require 'underscore'
module.exports =
class TimeReporter extends jasmine.Reporter
class TimeReporter extends jasmine.Reporter
constructor: ->
window.timedSpecs = []
window.timedSuites = {}
window.logLongestSpec = -> window.logLongestSpecs(1)
window.logLongestSpec = => @logLongestSpecs(1)
window.logLongestSpecs = (number) => @logLongestSpecs(number)
window.logLongestSuite = => @logLongestSuites(1)
window.logLongestSuites = (number) => @logLongestSuites(number)
window.logLongestSpecs = (number=10) =>
console.log "#{number} longest running specs:"
for spec in _.sortBy(window.timedSpecs, (spec) -> -spec.time)[0...number]
console.log "#{spec.time}ms"
console.log spec.description
logLongestSuites: (number=10, log) ->
log ?= (line) -> console.log(line)
log "Longest running suites:"
suites = _.map(window.timedSuites, (key, value) -> [value, key])
for suite in _.sortBy(suites, (suite) => -suite[1])[0...number]
log " #{suite[0]} (#{suite[1]}ms)"
window.logLongestSuite = -> window.logLongestSuites(1)
window.logLongestSuites = (number=10) =>
console.log "#{number} longest running suites:"
suites = _.map(window.timedSuites, (key, value) -> [value, key])
for suite in _.sortBy(suites, (suite) => -suite[1])[0...number]
console.log suite[0], suite[1]
logLongestSpecs: (number=10, log) ->
log ?= (line) -> console.log(line)
log "Longest running specs:"
for spec in _.sortBy(window.timedSpecs, (spec) -> -spec.time)[0...number]
log spec.description
reportSpecStarting: (spec) ->
stack = [spec.description]
@stack = [spec.description]
suite = spec.suite
while suite
stack.unshift suite.description
@stack.unshift suite.description
@suite = suite.description
suite = suite.parentSuite
@time = new Date().getTime()
reducer = (memo, description, index) ->
"#{memo}#{_.multiplyString(' ', index)}#{description}\n"
@description = _.reduce(stack, reducer, "")
reportSpecResults: (spec) ->
return unless @time? and @description?
return unless @time? and @stack?
duration = new Date().getTime() - @time
reducer = (memo, description, index) ->
if index is 0
"#{description} (#{duration}ms)\n"
else
"#{memo}#{_.multiplyString(' ', index)}#{description}\n"
description = _.reduce(@stack, reducer, '')
window.timedSpecs.push
description: @description
description: description
time: duration
name: spec.getFullName()
if timedSuites[@suite]
window.timedSuites[@suite] += duration
else
window.timedSuites[@suite] = duration
@time = null
@description = null
@stack = null