Jasmine 2.0 SpecRunner vs Karma

≯℡__Kan透↙ 提交于 2019-12-01 03:05:21

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)

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'
                };
            }
        };
    }
});

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:

  • In my computer node packages are install under (windows directory):
  • C:\Users\[UserName]\AppData\Roaming\npm\node_modules\karma-jasmine\lib:
  • open jasmine.js
    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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!