Image for post
Image for post

Why Is Unit Testing Required?

  1. Basically, it protects the existing code, which can be broken due to any changes.

Jasmin?

Jasmine is the most popular JavaScript testing framework in the Angular community. This is one the main features of Test Driven Development (TDD). This is the core framework that we will write our unit tests with. Basically, Jasmine and BDD try to describe a test method case in a human-readable pattern so that any user, including a non-technical person, can identify what is going on. Jasmine tests are written using JavaScript functions, which makes writing tests a nice extension of writing application code.

karma?

  1. Karma is a test automation tool for controlling the execution of our tests and what browser to perform them on. It also allows us to generate various reports on the results. For one or two tests, this may seem like overkill, but as an application grows larger and the number of units to test grows, it is important to organize, execute, and report on tests in an efficient manner. Karma is library-agnostic, so we could use other testing frameworks in combination with other tools (like code coverage reports, spy testing, e2e, etc.).
  2. In order to test our Angular application, we must create an environment for it to run in. We could use a browser like Chrome or Firefox to accomplish this (Karma supports in-browser testing), or we could use a browser less environment to test our application, which can offer us greater control over automating certain tasks and managing our testing workflow. PhantomJS provides a JavaScript API that allows us to create a headless DOM instance which can be used to bootstrap our Angular application. Then, using that DOM instance that is running our Angular application, we can run our tests.
  3. Karma is basically a tool which lets us spawn browsers and run Jasmine tests inside of them, which are executed from the command line. This results of the tests are also displayed in the command line. Karma also watches our development file changes and re-runs the tests automatically.

Difference between Jasmin & Karma?

Jasmin:

Initial Setup Required for Unit Tests

Basic “Helllo World“ Example

Built-In Matchers

Setup and Teardown (Basic Syntax)

What is Test Bed?

What is fixture?

Disabled and Focused tests

You can disable tests without commenting them our by just pre-pending x to the describe or it functions, like so:

Test case writing important points

  1. All imports should be present in your spec file.

Developer , Choreographer , Dancer , Traveler

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