Using ng test in Angular

Coding & Notes

Testing

I believe there is no right or wrong technology / library / framework to use when developing software except one that makes unit, integration and e2e testing easy to use.

  • the documentation of logic and behaviour through description of test cases — which I prefer reading than going through codes that someone else had written or myself did 6 months ago
  • writing alternatives test cases of scenarios we would otherwise not think of. For example, what happens if a value is null or undefined?

Initial System

In the book Getting Real, the authors argue that it’s good to cater for an empty system because that’s the first impression the user gets. During our coding phase, we already have dummy values. Testers often ping me for broken features that works correctly on my machine. After importing their database and going through all the steps to reproduce the error, most of the time, I uncover null or undefined values as the main culprit.

  • you become more confident to refactor your codes without fearing regression
  • your code base becomes easier to tingle with without worrying of breaking changes
  • can write logic codes separately and independent of the current system, making it easier for us to focus on the implementation now and worry about the integration later

Iterations

Of course, the biggest disadvantage is the time required to write down the tests. For developing a Minimum Viable Product (MVP), you want to iterate efficiently. In this regards, indeed, writing tests would be discouraged.

Code Coverage

Code Coverage
If condition

ngcli

I personally think the biggest reason to appreciate the use of Angular is thanks to its sublime command line tool ngcli. Even better is ng test for developing applications with plenty of business logic and validations. You can use a TDD approach.

Command Line
Karma Browser
  • describe and it functions can be written with arrow function
  • by default, ng test watches files for change and automatically run the suite — I find this Angular feature highly productive, as any change I do with the codes refreshes the tests
  • I don’t need to manually add karma, karma jasmine and protractor libraries — Once I create a new app, they are all included
  • Working on business logic and complex validation scenarios, I find the TDD approach highly effective ; it enables me to focus solely on the logic

How I write unit tests

Before writing the actual codes, I prepare a small set of data and try to solve the problem either on paper, in writing or using dry run tables.

sort date in desc

--

--

Senior Software Developer, Writer, Amateur Photographer, Reader

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abdallah Yashir

Abdallah Yashir

Senior Software Developer, Writer, Amateur Photographer, Reader