Testing On Angular

As a best practice we need to write tests for every code we write.There are lots of testing frameworks available to use depending on the programming language that we are using and the type of tests that we want to write(Front-end,Back-end,Functional tests,Integration tests)

Mocha and Jasmine are the most popular JavaScript test frameworks.

To start the development we can add Mocha to our project as a development dependency

$ npm install --save-dev mocha

We need several other testing tools to carry-out the testing.We may require an assertion library. Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

Chai can be installed as a development dependency as following

$ npm install --save-dev chai

We may also require to mock some data or functionalities to make sure unit tests are passing. Sinon is a good toll for that.Sinon allows you to replace the difficult parts of your tests with something that makes testing simple.Sinon helps eliminate complexity in tests by allowing you to easily create so called test-doubles.

If you want to test code making an Ajax call, how can you do that? You need to run a server and make sure it gives the exact response needed for your test. It’s complicated to set up, and makes writing and running unit tests difficult.

Sinon can be installed as following.

$ npm install sinon