I just started using Jasmine and I was able to use the SpecRunner from the Html just fine. However when I configured Karma I encountered a discrepancy:
descr
If you run npm install karma-jasmine@~0.2.1
after installing karma this will use the correct jasmine version (karma still hasn't updated to install the right version by default as the new adapter was only released a few days ago)
I checked Jasmine documentation site and I understand there are some important differences between 1.3 and 2.0 being one of those the way we declare matchers:
Based on this documentation (http://jasmine.github.io/2.0/custom_matcher.html) there is nothing wrong with:
jasmine.addMatchers({
toBeFive: function () {
return {
compare: function (actual, expected) {
return {
pass: actual === 5,
message: actual + ' is not exactly 5'
}
}
};
});
The issue is that karma is still running jasmine 1.3.1.
This is how I checked the version of Jasmine I was running:
C:\Users\[UserName]\AppData\Roaming\npm\node_modules\karma-jasmine\lib
:jasmine.version_ = { "major": 1, "minor": 3, "build": 1, "revision": 1354556913 };
I found that there are efforts in adapting karma to work with Jasmine 2.0.0:
https://github.com/r-park/karma-jasmine2-test
See this document https://www.packtpub.com/sites/default/files/downloads/7204OS_The_Future_Jasmine_2_0.pdf
2.0 breaks the way we do matchers
New syntax to create custom matchers Jasmine 2.0 comes with a new way of creating custom matchers. A lot of refactoring has been done under the hood, and the most important change is that internally Jasmine uses this same infrastructure to create its own built-in matchers.
Here is the new way of doing it.
jasmine.Expectation.addMatchers({
toBeAGoodInvestment: function() {
return {
compare: function (actual) {
var pass = actual.isGood();
var what = pass ? 'bad' : 'good';
return {
pass: pass,
message: 'Expected investment to be a '+ what +' investment'
};
}
};
}
});