Set Up Jasmine and Karma

Install karma-cli on your machine

npm install karma-cli -g

Add jasminekarma and karma-jasmine to your project’s dependencies

npm install jasmine karma karma-jasmine --save-dev

Create a karma configuration file

karma init

It starts init script where you can go with suggested settings. Once it has finished, you should manually update your project files in karma.conf.js, like so

files: [
'app/*.js',
'tests/*.js'
]

That configuration assumes that your main application logic is stored in app folder, and your Jasmine unit tests are located in the tests directory.

If your application depends on AngularJS you have to consider adding additional paths

'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',

In order to test if everything is setup properly, add your first test to the test directory

describe('first sample test', function() {
  it('should be true', function() {
    expect('bar').toBe('bar');
  });
});

and run karma

karma start

You should see that your test passed as below

2016-12-12_13h03_57

To stop karma, hit CTRL+C

Advertisements

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