a_sd-a
Last Updated: February 25, 2016
·
7.656K
· iilei

How to switch from jasmine 1.3.x to jasmine 2.0.0

Just a few notes from my experiences so far on migrating to jasmine 2.0.0 in an existing rails project, formerly using jasmine 1.3.0

Config dropped browser

In case in your jasmine_helper.rb is a line like config.browser = :phantomjs just remove it.

Rake task changed

Instead of running rake jasmine run RAILS_ENV=test rake jasmine – for running it headless just do RAILS_ENV=test rake jasmine:ci

Clock changed

No more need for jasmine.Clock.useMock();. Instead, use jasmine.clock().install(); and jasmine.clock().uninstall();

beforeEach(function() {
  jasmine.clock().install();
});

afterEach(function() {
  jasmine.clock().uninstall();
});

instead of jasmine.Clock.tick(42) now use jasmine.clock().tick(42)read more at the docs

Spies changed

Instead of .andCallThrough now it is .and.callThrough.
In doubt, try .and.fooBar instead of .andFooBar. – read more at the docs

Custom matchers

The Syntax to add custom matchers has changed significantly. Now it is recommended to have a spec_helper.js residing in the helpers folder adding custom matchers:

beforeEach(function() {
  jasmine.addMatchers(customMatchers);
});

and in a customMatchers object declared for example in a matchers.js file, also in the helpers folder.

var customMatchers = {
  toBeFoo: function(util, customEqualityTesters) {
    return {
      compare: function(actual, expected) {
        return {
          pass: actual === "foo"
        }
      },
      // optional negativeCompare (for not.toBeFoo)
      negativeCompare: function() {
        return {
          pass: actual !== "foo"
        }
      }
    }
  }
  // more custom matchers ...
}

3 Responses
Add your response

16858
17866

Wow thanks a ton I spent a lot of time trying to find out why it kept throwing jasmine.Clock.useMock is not a function, and this is the first page I found that explained it!

over 1 year ago ·
18194