Install sinon with jasmine and Karma

lately I wanted to use sinon within karma. So I simply did the following :

install sinon with bower : bower install sinon

then added the sinon.js to the karma files in the karma.conf.js like so :

files: [
'../bower_components/angular/angular.js',
'../bower_components/angular-mocks/angular-mocks.js',
'../src//.js',
'./unit/
/.js'
],

and use it in my jasmine test specs file…. but it didn’t work.

And here is the solution:

in a command prompt, navigate to the root of your project and type :

npm install karma-sinon --save-dev

then in your karma.conf.js file add the following:

frameworks: ['jasmine', 'sinon']

You can now use sinon in your jasmine files


var mySUT = {
    callCallback: function (cb) {
        cb();
    }
}

 describe('spies', function () {
        it('should spy on a callback', function(){
            var spy = sinon.spy();  
            
            mySUT.callCallback(spy);
            
            expect(spy.called).toBe(true);
            
            
        });
    })

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s