- async/await specs, which were not available yet in 2.0, are a better choice than callback-style async specs. It's also easier to convert Jasmine 1.x async specs directly to async/await.
- The custom matcher interface changed in 4.0. See the 4.0 migration guide for details on what changed, or the current custom matcher documentation for more information.
- Most of the breaking changes in future major releases have to do with APIs that didn't exist yet in 1.x. You can skip those changes entirely by upgrading directly to the latest version.
The rest of this upgrade guide is preserved more or less as it was at the time of the 2.0 release.
A number of things have changed with the release of jasmine 2.0 |
describe("Upgrading to jasmine 2.0", function() {
|
Custom Matchers |
describe("Custom Matchers", function() {
beforeEach(function() {
|
The |
/* was:
this.addMatchers({
*/
jasmine.addMatchers({
|
A matcher is set up a bit different now.
The factory receives a |
/* was:
toBeCustom: function(expected) {
var passed = this.actual == expected;
*/
toBeCustom: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var passed = actual == expected
|
The comparison should now return an object with For more information on the use of custom matchers. This page is intended to show the changes necessary to upgrade a 1.x suite to 2.0 |
/* was:
this.message = function() {
return [
'Expected ' + this.actual + ' to equal ' + expected,
'Expected ' + this.actual + ' not to equal ' + expected
];
};
return passed;
});
*/
return {
pass: passed,
message: 'Expected ' + actual + (passed ? '' : ' not') + ' to equal ' + expected
};
}
};
}
});
});
|
Use of custom matchers remains the same |
it("uses custom matchers", function() {
expect(1).toBeCustom(1);
});
});
|
Asynchronous Specs |
describe("Asynchronous Specs", function() {
|
We'll pretend this is asynchronous for our tests below |
var asyncSetThing,
somethingAsyncWithCallback = function(callback) {
asyncSetThing = true;
callback();
};
|
The |
/* was:
it("calls an async thing and waits", function() {
var asyncDone = false;
somethingAsyncWithCallback(function() {
asyncDone = true
});
|
While it used to be necessary to keep track of the async state in the spec itself. |
waitsFor(function() {
return asyncDone;
});
*/
|
By having a |
beforeEach(function(done) {
somethingAsyncWithCallback(done);
});
/*
runs(function() {
expect(asyncSetThing).toBeTruthy();
});
});
*/
it("will wait until async completes and calls done", function() {
expect(asyncSetThing).toBeTruthy();
});
});
|
Spies |
describe("Spies", function() {
it('should spy', function() {
var spy = jasmine.createSpy('spy');
|
All the ways to tell the spy how to behave are no longer their own attribute directly on the spy.
There is a single |
/* was:
spy.andCallThrough();
spy.andCallFake(function() {});
spy.andThrow('error');
spy.andReturn(1);
*/
spy.and.callThrough();
spy.and.callFake(function() {});
spy.and.throwError('error');
spy.and.returnValue(1);
|
Basic setup and check remains the same |
spy('foo');
spy('bar');
expect(spy).toHaveBeenCalledWith('foo');
|
Similarly to behaviors, more advanced call checks are on the |
/* was:
expect(spy.mostRecentCall.args).toEqual(['bar']);
expect(spy.callCount).toBe(2);
*/
expect(spy.calls.mostRecent().args).toEqual(['bar']);
expect(spy.calls.count()).toBe(2);
});
});
|
Clock |
describe("Clock", function() {
|
The jasmine mock clock is now an instanciated object and not a global,
and it is now |
beforeEach(function() {
/* was:
jasmine.Clock.useMock();
*/
jasmine.clock().install();
});
|
|
it("uses the clock similarly", function() {
/* was:
jasmine.Clock.tick();
*/
jasmine.clock().tick();
});
|
Jasmine 2.0 removes the ability for addons to dynamically add |
afterEach(function() {
jasmine.clock().uninstall();
});
});
});
|