pulsar/spec/time-reporter.coffee

53 lines
1.6 KiB
CoffeeScript
Raw Normal View History

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