From 7a89de077ba706ecfbb3c651e242749738088df1 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 10 Jan 2013 08:36:43 -0800 Subject: [PATCH] Log longest specs explicitly Instead of logging them to the console at the end of the run, add two new methods, logLongestSpec() and logLongestSpecs(number) to the window object that will print out the results. --- spec/time-reporter.coffee | 34 ++++++++++++++++++++++++++++++++++ vendor/jasmine-helper.coffee | 30 +----------------------------- 2 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 spec/time-reporter.coffee diff --git a/spec/time-reporter.coffee b/spec/time-reporter.coffee new file mode 100644 index 000000000..c2d12870c --- /dev/null +++ b/spec/time-reporter.coffee @@ -0,0 +1,34 @@ +_ = require 'underscore' + +module.exports = +class TimeReporter extends jasmine.Reporter + + timedSpecs: [] + + constructor: -> + window.logLongestSpec = -> window.logLongestSpecs(1) + window.logLongestSpecs = (number=10) => + console.log "#{number} longest running specs:" + for spec in _.sortBy(@timedSpecs, (spec) -> -spec.time)[0...number] + console.log "#{spec.time}ms" + console.log spec.description + + reportSpecStarting: (spec) -> + stack = [spec.description] + suite = spec.suite + while suite + stack.unshift suite.description + suite = suite.parentSuite + + @time = new Date().getTime() + reducer = (memo, description, index) -> + "#{memo}#{_.multiplyString(' ', index)}#{description}\n" + @description = _.reduce(stack, reducer, "") + + reportSpecResults: -> + return unless @time? and @description? + @timedSpecs.push + description: @description + time: new Date().getTime() - @time + @time = null + @description = null diff --git a/vendor/jasmine-helper.coffee b/vendor/jasmine-helper.coffee index 1c735ef8f..0defd1990 100644 --- a/vendor/jasmine-helper.coffee +++ b/vendor/jasmine-helper.coffee @@ -6,7 +6,7 @@ module.exports.runSpecSuite = (specSuite, logErrors=true) -> nakedLoad 'jasmine-focused' $ = require 'jquery' - _ = require 'underscore' + TimeReporter = require 'time-reporter' $('body').append $$ -> @div id: 'jasmine-content' @@ -20,34 +20,6 @@ module.exports.runSpecSuite = (specSuite, logErrors=true) -> jasmineEnv = jasmine.getEnv() jasmineEnv.addReporter(reporter) - class TimeReporter extends jasmine.Reporter - - timedSpecs: [] - - reportSpecStarting: (spec) -> - stack = [spec.description] - suite = spec.suite - while suite - stack.unshift suite.description - suite = suite.parentSuite - - @time = new Date().getTime() - @description = stack.join(' -> ') - - reportSpecResults: -> - return unless @time? and @description? - @timedSpecs.push - description: @description - time: new Date().getTime() - @time - @time = null - @description = null - - reportRunnerResults: -> - console.log '10 longest running specs:' - for spec in _.sortBy(@timedSpecs, (spec) -> -spec.time)[0...10] - console.log "#{spec.time}ms" - console.log spec.description - jasmineEnv.addReporter(new TimeReporter()) jasmineEnv.specFilter = (spec) -> reporter.specFilter(spec) jasmineEnv.execute()