Skip to content

Instantly share code, notes, and snippets.

@lightsofapollo
Created January 8, 2012 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lightsofapollo/1577413 to your computer and use it in GitHub Desktop.
Save lightsofapollo/1577413 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link
Author

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

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

@jipiboily
Copy link

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