Angular automated testing

In this article, we will learn more about automated testing in angular.

What is automated testing?
It is type of testing software without humans with lesser time. It helps you to catch the defects before releasing the software.

How to write a test coding in angular?
Before jumping into the test coding. Let see the different type of testing in angular.

Type of Testing in angular

  • Unit Testing (UT)
  • Integration Testing (IT)
  • End to End Testing (e2e)

Unit Testing
Test a component in Isolation, without external resources (e.g. file system, database, API endpoints).

  • Easiest to write
  • Superfast
  • Don’t give us much confidence.

Integration Tests
Test a component with external resources (e.g. file system, database, API endpoints)
Integration tests: component + template
Unit tests: component

End-to-end Tests

Test the entire application as a whole.

  • More confidence
  • Very slow
  • Very fragile

Let’s see the fundamental of Unit Testing
Tests are first-class citizens.

Clean Coding Practices

  • Small functions/ methods (10 lines of code or less)
  • Proper naming
  • Single responsibility

Example of Unit testing in angular

if(number < 0){
return 0;
return number + 1;

Use jasime javascript library to write the test code.
Common function using in angular is
describe() // suite
it() // spec
expect() // ecpected value

compute.spec.ts // test file

import {compute} from '.compute'
describe('compute', () =>{
it('should return O if input is negative', () => {
const result = compute(-1);

Run the code with “ng test compute”

 As you can see in the above image "Executed 1 of 1 success".
Run the ng test command create a browser instantiated and show like below. Karma will connected.
angular karma listener

After click on debug and see it in the console will reply the same of the test result. You can save it in a file for your track.

debug angular

This is how Unit testing will write. In next article will see the integration testing and later end to end testing.