Configuring Karma

Now that Karma is installed, we need to configure the following:

  • Jasmine as testing tool
  • Folders with the app code
  • Web browser launcher
  • Reporters

Setting up all the preceding parameters might result in a very time-consuming task, so we will use the Karma executable to configure everything automatically. In the practice-karma folder, run the following command:

$ karma init

This will prompt you a bunch of questions; just press enter to accept all the default configuration, and once everything is done, a karma.conf.js file will be created:

module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}

We need to specify a pattern for the files to be utilized in the testing process. To do this, apply the following changes to the files: [] property:

...
files: [
'specs/*.spec.js'
],
...

Now we have specified that every file that ends with .spec.js in its name will be processed by karma. Let's create the specs folder and inside, create the calculator.spec.js file:

$ mkdir specs
$ touch calculator.spec.js

You should have a project structure similar to the following:

.
└── node_modules
├── package.json
├── karma.spec.js
└── specs
└── calculator.spec.js

Now it's time to test things out by creating a testing example.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset