Dalorzo
Dalorzo

Reputation: 20024

Jasmine 2.0 SpecRunner vs Karma

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:

describe('Calculator', function(){
    var obj;
    beforeEach(function(){
        //initialize object
        obj = new Object();

        this.addMatchers({
            toBeFive: function () {
                return {
                    compare: function (actual, expected) {
                        return {
                            pass: actual === 5,
                            message: actual + ' is not exactly 5'
                        }
                    }
                };
            },

This piece of code does not work from the SpecRunner.html:

 this.addMatchers({

Instead I had to use this:

 jasmine.addMatchers({

This is what is include the specrunner:

 <!-- libs... -->
  <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script>
  <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script>
  <script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script>

  <!-- source files here... -->
  <script type="text/javascript" src="../../calculator/calculator.js"></script>

  <!-- test files here... -->
  <script type="text/javascript" src="spec/calculator/calculator-test.js"></script>

The error I get is:

TypeError: Object #<Object> has no method 'addMatchers'

Please note that Karma raise no errors but if I use jasmine.addMatchers({ it does.

Upvotes: 15

Views: 8061

Answers (3)

wheresrhys
wheresrhys

Reputation: 23550

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)

Upvotes: 10

Dalorzo
Dalorzo

Reputation: 20024

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

Upvotes: 4

Dustin Davis
Dustin Davis

Reputation: 14585

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

Upvotes: 4

Related Questions