Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jasmine reporter to find slow running tests
SlowDescribeReporter.prototype = {
threshold: 400,
reportRunnerStarting: function(runner){
var i, len, suites = runner.suites_, self = this;
this.topLevels = {};
for(i = 0, len = suites.length; i < len; i++){
(function(exec){
suites[i].execute = function(){
self.topLevels[this.id].start = new Date();
exec.apply(this, arguments);
};
}(suites[i].execute));
this.topLevels[suites[i].id] = {
start: null,
stop: null
}
}
},
reportRunnerResults: function(runner){
},
resultsForSpecs: function(){
},
reportSuiteResults: function(suite){
var start, stop, ms;
if(this.topLevels[suite.id]){
start = this.topLevels[suite.id].start;
stop = this.topLevels[suite.id].stop = new Date();
if(!start || !stop){
console.log(suite.description, 'failed', start, stop);
} else {
ms = (stop.getTime() - start.getTime());
if(ms > this.threshold){
console.log(suite.getFullName(), 'completed in ', ms / 1000, 'seconds (', ms, ' ms)');
}
}
}
},
reportSpecStarting: function(spec){
},
reportSpecResults: function(spec){
}
};
@lightsofapollo

This comment has been minimized.

Copy link
Owner Author

@lightsofapollo lightsofapollo commented Jan 8, 2012

In case this was not clear this is a quick hack to find slow running describe blocks inside of your test suite.
There might be a better way to monitor the "start" of a suite but I could not find one easily so I simply override execute per suite and record the start time there.

@lightsofapollo

This comment has been minimized.

Copy link
Owner Author

@lightsofapollo lightsofapollo commented Jan 8, 2012

You can use this to add it to your env (like a spec helper)

jasmine.getEnv().reporter.addReporter(new SlowDescribeReporter());
@jipiboily

This comment has been minimized.

Copy link

@jipiboily jipiboily commented Jan 14, 2016

I wrote something simple to find the slowest running specs in Jasmine 2.3, might be useful to others: http://jipiboily.com/how-to-know-jasmine-specs-are-slow/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment