This is a karma plugin (framework) which extends jasmine version 2 to allow some of the old tests to pass. It prints deprecation warnings at the end of tests run.
There are breaking changes, though, which cannot be shimed:
- Clock needs to be removed after usage with
jasmine.clock.uninstall()
. - Object equality assertion is stricter in Jasmine 2 regarding keys with
undefined
value. This means thattoEqual()
,toHaveBeenCalledWith()
, similar matchers will fail in cases likeexpect({}).toEqual({ a: undefined });
.
See how to migrate here: http://jasmine.github.io/2.3/upgrading.html
Add jasmine1-shim
to karma.conf.js
module.exports = function(config) {
config.set({
// ...
// Optional as "plugins" is populated automatically.
plugins: [
'karma-jasmine',
'karma-jasmine1-shim'
],
frameworks: ['jasmine', 'jasmine1-shim'],
// ...
});
};
Start by adding shim to your frameworks list of Karma’s configuration. Your tests should pass in most of the cases. You will see a list of messages indicating old methods and properties being used that are no longer available in new Jasmine version.
You could fix them by hand if you have just a few tests. Otherwise, sed
will
help. Following are shell commands that will automate migration. Run them in
appropriate directory, like test/spec/
.
First, the easy targets:
find . -name '*.js' \
| xargs sed -i.bak \
-e 's/\.mostRecentCall/.calls.mostRecent()/g' \
-e 's/\.mostRecentCall/.calls.mostRecent()/g' \
-e 's/\.callCount/.calls.count()/g' \
-e 's/\.calls.length/.calls.count()/g' \
-e 's/\.andCallThrough()/.and.callThrough()/g' \
-e 's/\.argsForCall/.calls.allArgs()/g' \
-e 's/\.calls\[0\]/.calls.first()/g' \
-e 's/\.andCallFake(/.and.callFake(/g' \
-e 's/\.andReturn(/.and.returnValue(/g'
Your tests should still pass. At least failing tests count should not increase.
Above will replace the most obvious methods and properties of spies. Next on the
list, are .reset()
and .calls
property. It would be presumptuous to treat
every occurrence as indication of spy, thus it’s recommended to run these
individually and then carefully inspect diffs.
$ find . -name '*.js' | xargs sed -i.bak -e 's/\.reset()/.calls.reset()/g' $ find . -name '*.js' | xargs sed -i.bak -e 's/\.calls\([^\.]\)/.calls.all()\1/g'
And finally, you should migrate your custom matchers. For how to, see http://jasmine.github.io/2.3/upgrading.html
If all seems fine at the end, you can remove backed up files rm *.bak
.