Sharing Top Content from the Angular-sphere.

Writing Unit Tests in Angular 2

Writing Unit Tests in #Angular 2  via @discoversdks #javascript #testing #Programming #coding

  • Writing Unit Tests in Angular 2 – Using Jasmine and Karma – – When you create a new project through CLI, you will find the “e2e” folder that contains the end-to-end tests using the Protractor.
  • Testing guards you against the changes that break existing code (‘regressions’) So for example, if your application is open-source, and it gets new changes by the members of your team (through pull requests, for instance), then tests can help you determine if the specific changes are suitable for the development…
  • Pipes and Services should be tested with unit testing, but components need to be tested with Angular testing utilities (through the TestBed class and helper functions from @angular/core/testing), because components interact with other components, so unit tests can’t reveal that information to us.
  • Unit tests are isolated in the sense that they examine an instance of a class (or object) by itself without any reference to other dependencies, so you can test the important codes separately, meaning you can use both approaches in a same application.
  • Even though AngularJS 2 also provides its own Testing Utilities for creating a test environment, they are not very sophisticated compared to Jasmine, Karma and Protractor (for end-to-end).

Writing Unit Tests in AngularJS 2 – Using Jasmine and Karma – DiscoverSDK Blog

The testing of the smallest parts of the application through unit testing may seem strange to new developers.

When the applications reach the production level, then the developers start separating the smallest testable parts of an application for the testing (let’s say functions for printing a text on the console), as it assures that new changes in the development mode don’t break the core features. Usually, a small piece of source code from several source files are grouped together (especially in the case of unit testing) for the testing, and it happens mostly in automation.

Imagine a scenario where you have a functioning application that is hosted on Github for other developers to see, but it reached the production level and yet you still want to receive changes (in the form of pull requests) from other developers. Your application is now being used by major corporations or several commercial projects where even the slightest mistake in your code can break their application. In this scenario, only  unit-testing can provide you with a tool to evaluate the new changes if it matches with your previous desire or expectations of your application.

In AngularJS 2, testing is provided in two ways: unit testing and end-to-end testing. The top-down approach is end-to-end, where the tests are run against your application running in a real browser and so interacting with it as a user would. Testing in isolation is unit-testing…

Writing Unit Tests in Angular 2