Head start with Testing in Angular with Jasmine
Tags: angular, testing, typescript, codenewbie
Published: 2020-10-04 16:28:07


I was working for several months with Jest and Vue, but nowadays I'm working with Angular and the testing is part of my learning process.

I will explain a little bit about Jasmine and how to write a test for typescript classes, if you found something wrong please notify by twitter @danywalls.

## The testing in Angular

Angular was build to be testable, it comes with Protractor, Jasmine, and Karma to help us to build unit and E2E tests. 

The Angular-cli provides scaffolding to build and run those tests and also these tools can be configured to run your test during the development or build process.

## Unit tests

The unit test is to check a basic piece or units of code, that means our test only tests one responsibility of our code.

The Unit test is used for test functions, objects, and more.

The jasmine framework is the framework that angular comes to write these tests.

## Jasmine functions
Jasmine framework comes with several functions, you don't need to memorize everyone but with basic, you can get started with describe, it, and expect.

*describe* help to group a series of tests, aka as test suite it takes 2 parameters, a string and a callback like.

```typescript
describe("my group of invoice tests", () => {} );
```

_it_ function help create a specific test, close similar to describe but is used to write the test.

```typescript
it("the number should be positive", () => {});
```

expect function play with our test that confirms that the test work as expected, it asserts our code. the expect function expect, is where I pass in the value and the matcher function is where pass the expected value.

Some of the common matcher functions are toBe(), toBeTrurty() or toEqual().

```typescript
expect(true).toBeThruthy();
```
The unit test files convention is filetotest.spec.ts by default angular-cli generate app.spect.ts and you run the test with ng test command.

## Write basic unit test with jasmine

Time to put my skill to test and practice what I just learned writing a small test using describe, it for the Gif class.

```typescript
export class Gif {
  private _url: string;
  urlValidator = new RegExp(/^(ftp|http|https):\/\/[^ "]+$/);
  public get url(): string {
    return this._url;
  }
  public set url(v: string) {
    this._url = v;
  }
  public get isvalid(): boolean {
    return this.urlValidator.test(this.url);
  }
}
```

What I can test? 

- If the public properties url and isValid has the expected value.
- The behavior like when a not valid url the gif is invalid.
- If can create an instance.
- If the URL can be is assigned.

Let's go!

1- Import the Gif class and describe it to define the test group.

```typescript
import { Gif } from './gif';
describe('Gif Class tests', () => {
	
}
```
2- Using it function define the test about a valid class constructor and with expect validate it is not null.

```typescript
import { Gif } from './gif';
describe('The Gif', () => {
  it('should to have a valid constructor', () => {
    const gif = new Gif();
    expect(gif).not.toBeNull();
  });
}
```
Run our tests with ng test from CLI.

```typescript
Chrome 85.0.4183.121 (Windows 10): Executed 1 of 1 SUCCESS (0.035 secs / 0.01 secs)
TOTAL: 1 SUCCESS
```

Adding the rest tests for the public properties.

```typescript
import { Gif } from './gif';
describe('The Gif', () => {
  it('should to have a valid constructor', () => {
    const gif = new Gif();
    expect(gif).not.toBeNull();
  });

  it('should set the url', () => {
    const gif = new Gif();
    gif.url = 'http://google.com';
    expect(gif.url).not.toBeNull();
  });

  it('should be invalid gif for wrong url', () => {
    const gif = new Gif();
    gif.url = 'ht';
    expect(gif.isvalid).toBe(false);
  });

  it('should be valid gif for valid url', () => {
    const gif = new Gif();
    gif.url = 'http://www.google.com';
    expect(gif.isvalid).toBe(true);
  });

  it('should asign the url string', () => {
    const gif = new Gif();
    const expectedUrl = 'http://www.google.com';
    gif.url = expectedUrl;
    expect(gif.url).toEqual(expectedUrl);
  });
});
```

Then run the ng test command get the results

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mai9odjzi7ocdhiv3ldy.PNG)

### Refactor tests with beforeEach and afterEach

If we read the code, the gif instance is created for each test, it is not the best approach. Jasmine provide a solution for the _setup_ of test with  beforeEach() and afterEach() function.

The beforeEach function help run code before each test. In this case, we declare a variable as gif type and initialize it for each test.

```javascript
describe('The Gif', () => {
  let gif: Gif = null;
  beforeEach(() => {
    gif = new Gif();
  });
    ... 
```

The same is for afterEach, assign the gif instance to null.

```javascript
afterEach(() => {
    gif = null;
  });
```

The final code looks like:

```typescript
import { Gif } from './gif';
describe('The Gif', () => {
  let gif: Gif = null;
  beforeEach(() => {
    gif = new Gif();
  });
  it('should to have a valid constructor', () => {
    expect(gif).not.toBeNull();
  });

  it('should set the url', () => {
    gif.url = 'http://google.com';
    expect(gif.url).not.toBeNull();
  });

  it('should be invalid for wrong url', () => {
    gif.url = 'ht';
    expect(gif.isvalid).toBe(false);
  });

  it('should be valid for valid url', () => {
    gif.url = 'http://www.google.com';
    expect(gif.isvalid).toBe(true);
  });

  it('should asign the url string', () => {
    const expectedUrl = 'http://www.google.com';
    gif.url = expectedUrl;
    expect(gif.url).toEqual(expectedUrl);
  });
  afterEach(() => {
    gif = null;
  });
});

```

Run the new tests with the _ng test_ command from your terminal.

```bash
ng test
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t53j0ipjw06kgoevfzf9.PNG)

## Done!

You should feel confident for write your first tests using Jasmine for your Angular Apps.