Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 763
Dalorzo Avatar asked Jan 02 '14 13:01

Dalorzo


People also ask

Is Jasmine and karma the same?

Jasmine can be classified as a tool in the "Javascript Testing Framework" category, while Karma is grouped under "Browser Testing". "Can also be used for tdd " is the primary reason why developers consider Jasmine over the competitors, whereas "Test Runner" was stated as the key factor in picking Karma.

Does karma use Jasmine?

We can run Jasmine tests in a browser ourselves by setting up and loading a HTML file, but more commonly we use a command-line tool called Karma. Karma handles the process of creating HTML files, opening browsers and running tests and returning the results of those tests to the command line.

What is Karma and Jasmine?

Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests. Karma is a test runner that fits all our needs in the angular framework.

Why do we use Karma and Jasmine?

Jasmine is a behavior-driven development framework for testing JavaScript code that plays very well with Karma. Similar to Karma, it's also the recommended testing framework within the Angular documentation as it's setup for you with the Angular CLI. Jasmine is also dependency free and doesn't require a DOM.


1 Answers

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)

like image 81
wheresrhys Avatar answered Oct 13 '22 21:10

wheresrhys