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.

Generating a SpecRunner for jasmine using grunt

When i add and remove tests i often have to create/edit a SpecRunner.html file. I know i can run a test server using karma. But for developing tests i prefer to inspect the results in the browser. I’v

Updating the version of Jasmine used in karma-jasmine

Questions How can I update the version of Jasmine used when running Jasmine via Karma using the karma-jasmine plugin? Will Jasmine only get updated whenever the karma-jasmine plugin integrates a newe

Jasmine async test passes in SpecRunner but fails from command line (grunt-contrib-jasmine)

I have a test for a method that will pass if I run it via the SpecRunner but will fail if I run the test from the command line (e.g. grunt jasmine). The test is shown below. The doSomething() method m

Test scroll position with Karma, PhantomJS, Jasmine

I’m using Karma with Jasmine configured with the PhantomJS browser as detailed in browser section of Karma’s config docs. I’m testing a scroll behavior in the page by inspecting the $(‘body,html’).scr

Jasmine/Karma not finding Angular modules

I’m trying unit testing with Jasmine and Karma, but for some reason my Angular modules cannot be found. I’ve modified code from examples: karma.cofig.js: files: [ ‘lib/angular.js’, ‘lib/angular-mocks.

Karma/Jasmine times out without running tests

I’m trying to run Karma/Jasmine from Grunt on a project generated with http://newtriks.com/2013/12/31/automating-react-with-yeoman-and-grunt/ Karma launches PhantomJS (or Chrome) and, depending on sin

karma-jasmine Cannot read property ‘clearTimeout’

I can’t seem to get karma-jasmine working. If I run karma start I get this error: Chrome 37.0.2062 (Windows 8.1) ERROR Uncaught TypeError: Cannot read property ‘clearTimeout’ of undefined at [censore

Need proper html reporter for karma jasmine

I have a fairly simple karma.config.js file basePath = ‘../’; files = [ JASMINE, JASMINE_ADAPTER, ‘js/lib/angular*.js’, ‘test/lib/angular/angular-mocks.js’, ‘js/**/*.js’, ‘test/unit/**/*.js’ ]; autoWa

Generate jasmine report using Karma Runner

I want to obtain a report of all successful jasmine specs runned with karma, something like you obtain when using jasmine alone. Is there anyway to achieve this?

Configuring jasmine-jquery to work with Karma

I am trying to configure Karma to use jasmine-jquery without success … In my karma.conf.js file I have configured the framework: frameworks: [‘jasmine’], and have loaded the required dependencies:

Answers

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

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)