Getting started with Karma for AngularJS Testing

Install NodeJS

  1. https://nodejs.org/en/download/
  2. Install to your computer
  3. Ensure your npm command is run behind proxy server
  4. if your have any problems, please follow this instruction
    http://jjasonclark.com/how-to-setup-node-behind-web-proxy/
    http://stackoverflow.com/questions/25660936/using-npm-behind-corporate-proxy-pac

Skipping create package.json

I will install module by my self, so package.json will not include in this article.

Install Karma

  1. npm install karma-cli -g
  2. npm install karma — save-dev
  3. npm install karma-jasmine — save-dev
  4. npm install karma-chrome-launcher — save-dev

Creating karma config file

  1. karma init karma.conf.js

Answer the prompts as follows:

  • Which testing framework do you want to use?

Hit return to accept the default value i.e. jasmine.

  • Do you want to use Require.js ?

Hit return to accept the default value i.e. no.

  • Do you want to capture any browsers automatically ?

Hit return to accept the default value i.e. Chrome.

  • What is the location of your source and test files ?

Enter the following value:

tests/*.test.js

Don’t worry if you accidentally skipped this, we can directly edit the config file at a later stage.

  • Should any of the files included by the previous patterns be excluded ?

Hit return to accept the default value.

  • Do you want Karma to watch all the files and run the tests on change ?

Hit return to accept the default value. i.e. yes

The config file called karma.conf.js will be created in the root folder.

Creating test

  1. create file test.js into test directive
describe(“A suite”, function() {
     it(“contains spec with an expectation”, function() {
       expect(true).toBe(true);
     });
});

Execute test

karma start karma.conf.js

Reporter

http://stackoverflow.com/questions/17289423/need-proper-reporter-for-karma-jasmine

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.