Unit testing Jasmine

We have two types of testing.

Unit test and end to end test. Today we will focus only in Unit testing.

Also we will use Jasmin framework.

As we can see on official documentation page of Jasmin we have complete description of the framework:

Jasmine is a behavior-driven development framework for testing JavaScript code.
It does not depend on any other JavaScript frameworks. It does not require a DOM.
And it has a clean, obvious syntax so that you can easily write tests.

Unit Testing

Unit tests can test the functionality of basic parts or units of code. Each unit test should only test a single responsibility of the source code.

The first part of unit testing is to check if our testing environment is set up correctly. In order to do that we create a simple test like the
following example:

describe('super basic test', () => {
it('true is true', () => {
expect(true).toEqual(true);
});
});

All we are doing here is to check if the value true is equal to Boolean value true.

Real World Example:
Let's say that we want to search if the following service is working properly:

In myservice.service.ts we have my service:

@Injectable()
export class myService {...}

In our testing file: myservice.service.spec.ts we will have this:

import { TestBed, inject } from '@angular/core/testing';

import { myService } from './myService.service';

describe('myService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [myService]
});
});

it('should be created', inject([myService], (service: myService) => {
expect(service).toBeTruthy();
}));
});

To configure a testing module, we use Angular’s TestBed. TestBed is Angular’s primary API to configure and
initialize environments for unit testing and provides methods for creating components and services in unit tests.
We can create a module that overrides the actual dependencies with testing dependencies, using TestBed.configureTestingModule().

To execute our tests we enter on command line where we have our project the following command: ng test

But what is these words:
Describe, it, expect?

Describe
The describe function is used to group together a series of tests. This group of tests is known as a suite. The
describe function takes two parameters, a string and a callback function, in the following format:
describe(message describing the test suite, describe callback);
You can nest as many describe functions as you want to organize your tests. You can have as many describe
functions as you want. The number of describe functions depends on how you want to organize your tests into suites.

It
The it function is used when we want to create a specific test. Like the describe function, the it function takes two
parameters, a string and a callback function. And also like the describe function, we can store executable code in the
it function. The it function can be used to separate each test and uses the following format: it(message
describing the test, it callback);

Expect
The expect function is where you want to write the code for the test to work. These lines of code are also know as
assertion, because you are asserting something as being true. In Jasmine, the assertion is in two parts. The expect
and the matcher. The expect function is where you pass in the actual value, for example, a Boolean value true. The
second part of the assertion is the matcher function, which is where we put the expected value.
Matcher functions examples include toBe(), toContain(), and toThrow(), toEqual(), toBeTruthy(), toBeNull(), and
more.

More on this subject you can learn on : https://jasmine.github.io/

Σχόλια