JP Boily

Founder of Metrics Watch

Jack of all trade SaaS consultant (but mostly code, analytics & marketing)

January 14, 2016

How to know which Jasmine specs are slow?

January 14, 2016 - JP -  

So you have this nice Jasmine suite that’s very useful and maybe even proud of…but it takes over a minute to run? It seems to get stuck at one or a few specs but you can’t figure out which one? I had that exact problem this morning while working on my SaaS, Metrics Watch (near real-time alerts for Google Analytics).

Let me help you!

Add this helper:

// This works under Jasmine 2.3
var slowSpecsReporter = {
  specStarted: function (result) {
    this.specStartTime = Date.now();
  },
  specDone: function (result) {
    var seconds = (Date.now() - this.specStartTime) / 1000;
    if (seconds > 0.5) {
      console.log(
        "WARNING - This spec took " +
          seconds +
          ' seconds: "' +
          result.fullName +
          '"'
      );
    }
  },
};
jasmine.getEnv().addReporter(slowSpecsReporter);

In my case, I am using jasmine-rails, so to have this helper running I had to create a file named specs/javascripts/support/jasmine-slow-spec-reporter.js and edit the jasmine.yml so it looks more like this:

helpers:
  - "helpers/**/*.{js.coffee,js,coffee}"
  - "support/jasmine-ajax.js"
  - "support/jasmine-time-reporter.js"

Now you’ll get console outputs on slow spec with the full name of the spec, you can now find out those slow specs and get your suite to run in less than 5 seconds instead of over a minute!

That’s the easy part though, the hard part is usually to figure out WHY your specs are slow, at least, in my case it was not that obvious. Want to know how I made it faster? Subscribe to my email list (the box at the bottom right) and I’ll let you know how I did for my React & Redux project soon! You don’t want to figure it out and need a hand now speeding up your test suites (Javascript or Ruby)? Hire me ;)