Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (2024)

Test Driven Development is a programming practice that has been preached and promoted by every developer community on the planet.And yet it's a routine that is largely neglected by a developer while learning a new framework. Writing unit tests from day one will help you to write better code, spot bugs with ease, and maintain a better development workflow.

Test-Driven Development in Angular

Angular, being a full-fledged front-end development platform, has its own set of tools for testing. We will be using the following tools in this tutorial:

  • Jasmine Framework. Jasmine is a popular behavior-driven testing framework for JavaScript. With Jasmine, you can write tests that are more expressive and straightforward. Here is an example to get started.
1
 it('should have a defined component', () => {
2
 expect(component).toBeDefined();
3
 });
  • Karma Test Runner. Karma is a tool that lets you test your application on multiple browsers. Karma has plugins for browsers like Chrome, Firefox, Safari, and many others. But I prefer using a headless browser for testing. A headless browser lacks a GUI, and that way, you can keep the test results inside your terminal. In this tutorial, we will configure Karma to run with Chrome and, optionally, a headless version of Chrome.
  • Angular Testing Utilities. Angular testing utilities provide you a library to create a test environment for your application. Classes such asTestBedandComponentFixturesand helper functions such asasyncandfakeAsyncare part of the@angular/core/testingpackage. Getting acquainted with these utilities is necessary if you want to write tests that reveal how your components interact with their own template, services, and other components.

We are not going to cover functional tests using Protractor in this tutorial. Protractor is a popular end-to-end test framework that interacts with the application's UI using an actual browser.

In this tutorial, we are more concerned about testing components and the component's logic. However, we will be writing a couple of tests that demonstrate basic UI interaction using the Jasmine framework.

Our Goal

The goal of this tutorial is to create the front-end for a Pastebin application in a test-driven development environment. In this tutorial, we will follow the popular TDD mantra, which is "red/green/refactor". We will write tests that initially fail (red) and then work on our application code to make them pass (green). We shall refactor our code when it starts to stink, meaning that it gets bloated and ugly.

We will be writing tests for components, their templates, services, and the Pastebin class. The image below illustrates the structure of our Pastebin application. The items that are grayed out will be discussed in the second part of the tutorial.

Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (1)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (2)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (3)

In the first part of the series, we will solely concentrate on setting up the testing environment and writing basic tests for components. Angular is a component-based framework; therefore, it is a good idea to spend some time getting acquainted with writing tests for components. In the second part of the series, we will write more complex tests for components, components with inputs, routed components, and services. By the end of the series, we will have a fully functioning Pastebin application that looks like this.

Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (4)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (5)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (6)
Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (7)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (8)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (9)
Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (10)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (11)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (12)

In this tutorial, you will learn how to:

  • configure Jasmine and Karma
  • create a Pastebin class that represents an individual paste
  • create a bare-bones PastebinService
  • create two components, Pastebin and AddPaste
  • write unit tests

The entire code for the tutorial is available on Github.

1
https://github.com/blizzerand/pastebin-angular

Clone the repo and feel free to check out the code if you are in doubt at any stage of this tutorial. Let's get started!

Configuring Jasmine and Karma

The developers at Angular have made it easy for us to set up our test environment. To get started, we need to install Angular first. I prefer using the Angular-CLI. It's an all-in-one solution that takes care of creating, generating, building and testing your Angular project.

1
ng new Pastebin

Here is the directory structure created by Angular-CLI.

Since our interests are inclined more towards the testing aspects in Angular, we need to look out for two types of files.

karma.conf.jsis the configuration file for the Karma test runner and the only configuration file that we will need for writing unit tests in Angular. By default, Chrome is the default browser-launcher used by Karma to capture tests. We will create a custom launcher for running the headless Chrome and add it to thebrowsersarray.

1
/*karma.conf.js*/
2
browsers: ['Chrome','ChromeNoSandboxHeadless'],
3
4
customLaunchers: {
5
 ChromeNoSandboxHeadless: {
6
 base: 'Chrome',
7
 flags: [
8
 '--no-sandbox',
9
 // See https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
10
 '--headless',
11
 '--disable-gpu',
12
 // Without a remote debugging port, Google Chrome exits immediately.
13
 ' --remote-debugging-port=9222',
14
 ],
15
 },
16
},

The other type of file that we need to look out for is anything that ends with.spec.ts. By convention, the tests written in Jasmine are called specs. All the test specs should be located inside the application'ssrc/app/directory because that's where Karma looks for the test specs. If you create a new component or a service, it is important that you place your test specs inside the same directory that the code for the component or service resides in.

Theng newcommand has created anapp.component.spec.tsfile for ourapp.component.ts. Feel free to open it up and have a good look at the Jasmine tests in Angular. Even if the code doesn't make any sense, that is fine. We will keep AppComponent as it is for now and use it to host the routes at some later point in the tutorial.

Creating the Pastebin Class

We need a Pastebin class to model our Pastebin inside the components and tests. You can create one using the Angular-CLI.

1
ng generate class Pastebin

Add the following logic to Pastebin.ts:

1
export class Pastebin {
2
3
 id: number;
4
 title: string;
5
language: string;
6
paste: string;
7
8
constructor(values: Object = {}) {
9
 Object.assign(this, values);
10
 }
11
12
}
13
14
 export const Languages = ["Ruby","Java", "JavaScript", "C", "Cpp"];
15
16
 

We have defined a Pastebin class, and each instance of this class will have the following properties:

  • id
  • title
  • language
  • paste

Create another file calledpastebin.spec.tsfor the test suite.

1
/* pastebin.spec.ts */
2
3
//import the Pastebin class
4
import { Pastebin } from './pastebin';
5
6
describe('Pastebin', () => {
7
 it('should create an instance of Pastebin',() => {
8
 expect(new Pastebin()).toBeTruthy();
9
});
10
})

The test suite starts with adescribeblock, which is a global Jasmine function that accepts two parameters. The first parameter is the title of thetest suite, and the second one is its actual implementation. The specs are defined using anitfunction that takes two parameters, similar to that of thedescribeblock.

Multiple specs (itblocks) can be nested inside a test suite (describeblock). However, ensure that the test suite titles are named in such a way that theyare unambiguous and more readable because they are meant to serve as a documentation for the reader.

Expectations, implemented usingtheexpectfunction, are used by Jasmine to determine whether a spec should pass or fail. Theexpectfunction takes a parameter which is known as the actual value. It is then chained with another function that takes the expected value. These functions are called matcher functions, and we will be using the matcher functions liketoBeTruthy(),toBeDefined(),toBe(), andtoContain()a lot in this tutorial.

1
 expect(new Pastebin()).toBeTruthy();

So with this code, we've created a new instance of the Pastebin class and expect it to be true. Let's add another spec to confirm that the Pastebin model works as intended.

1
it('should accept values', () => {
2
let pastebin = new Pastebin();
3
pastebin = {
4
id: 111,
5
title: "Hello world",
6
language: "Ruby",
7
paste: 'print "Hello"',
8
}
9
expect(pastebin.id).toEqual(111);
10
expect(pastebin.language).toEqual("Ruby");
11
expect(pastebin.paste).toEqual('print "Hello"');
12
});
13

We've instantiated the Pastebin class and added a few expectations to our test spec. Runng testto verify that all the tests are green.

Creating a Bare-Bones Service

Generate a service using the below command.

1
ng generate service pastebin

PastebinService will host the logic for sending HTTP requests to the server; however, we don't havea server APIfor the application we are building. Therefore,we are going to simulate the server communication using a module known asInMemoryWebApiModule.

Setting Up Angular-in-Memory-Web-API

Install angular-in-memory-web-api via npm:

1
npm install angular-in-memory-web-api --save

Update AppModule with this version.

1
/* app.module.ts */
2
3
import { BrowserModule } from '@angular/platform-browser';
4
import { NgModule } from '@angular/core';
5
6
//Components
7
import { AppComponent } from './app.component';
8
9
//Service for Pastebin
10
11
import { PastebinService } from "./pastebin.service";
12
13
//Modules used in this tutorial
14
import { HttpModule } from '@angular/http';
15
16
//In memory Web api to simulate an http server
17
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
18
import { InMemoryDataService } from './in-memory-data.service';
19
20
21
@NgModule({
22
 declarations: [
23
 AppComponent,
24
 ],
25
 
26
 imports: [
27
 BrowserModule, 
28
 HttpModule,
29
 InMemoryWebApiModule.forRoot(InMemoryDataService),
30
 ],
31
 providers: [PastebinService],
32
 bootstrap: [AppComponent]
33
})
34
export class AppModule { }

Create an InMemoryDataService that implementsInMemoryDbService.

1
/*in-memory-data.service.ts*/
2
import { InMemoryDbService } from 'angular-in-memory-web-api';
3
import { Pastebin } from './pastebin';
4
export class InMemoryDataService implements InMemoryDbService {
5
 createDb() {
6
 const pastebin:Pastebin[] = [
7
 { id: 0, title: "Hello world Ruby", language: "Ruby", paste: 'puts "Hello World"' },
8
 {id: 1, title: "Hello world C", language: "C", paste: 'printf("Hello world");'},
9
 {id: 2, title: "Hello world CPP", language: "C++", paste: 'cout<<"Hello world";'},
10
 {id: 3, title: "Hello world Javascript", language: "JavaScript", paste: 'console.log("Hello world")'}
11
 
12
 ];
13
 return {pastebin};
14
 }
15
}

Here,pastebinis an array of sample pastes that will be returned or updated when we perform an HTTP action likehttp.getorhttp.post.

1
/*pastebin.service.ts */
2
import { Injectable } from '@angular/core';
3
import { Pastebin } from './pastebin';
4
import { Http, Headers } from '@angular/http';
5
import 'rxjs/add/operator/toPromise';
6
7
@Injectable()
8
export class PastebinService {
9
10
// The project uses InMemoryWebApi to handle the Server API. 
11
// Here "api/pastebin" simulates a Server API url 
12
 private pastebinUrl = "api/pastebin";
13
 private headers = new Headers({'Content-Type': "application/json"});
14
 constructor(private https: Http) { }
15
16
// getPastebin() performs http.get() and returns a promise
17
 public getPastebin():Promise<any> {
18
 return this.http.get(this.pastebinUrl)
19
 .toPromise()
20
 .then(response => response.json().data)
21
 .catch(this.handleError);
22
 
23
 }
24
25
 private handleError(error: any): Promise<any> {
26
 console.error('An error occurred', error); 
27
 return Promise.reject(error.message || error);
28
 }
29
}

ThegetPastebin()method makes an HTTP.get request and returns a promise that resolves to an array of Pastebin objects returned by the server.

If you get a No provider for HTTP error while running a spec, you need to import the HTTPModule to the concerned spec file.

Getting Started With Components

Components are the most basic building block of an UI in an Angular application. An Angular application is a tree of Angular components.
— Angular Documentation

As highlighted earlier in the Overview section, we will be working on two components in this tutorial:PastebinComponentandAddPasteComponent. ThePastebin component consists ofa table structure that lists all the paste retrieved from theserver. The AddPaste component holds the logic for creation of new pastes.

Designing and Testing the Pastebin Component

Go ahead and generate the components using Angular-CLI.

1
ng g component --spec=false Pastebin

The--spec=falseoption tells the Angular-CLI not to create a spec file. This is because we want to write unit tests for components from scratch. Create apastebin.component.spec.tsfile inside thepastebin-componentfolder.

Here's the code for pastebin.component.spec.ts.

1
import { TestBed, ComponentFixture, async } from '@angular/core/testing';
2
import { DebugElement } from '@angular/core';
3
import { PastebinComponent } from './pastebin.component';
4
import { By } from '@angular/platform-browser';
5
import { Pastebin, Languages } from '../pastebin';
6
7
//Modules used for testing
8
import { HttpModule } from '@angular/http';
9
10
describe('PastebinComponent', () => {
11
12
//Typescript declarations.
13
 let comp: PastebinComponent;
14
 let fixture: ComponentFixture<PastebinComponent>;
15
 let de: DebugElement;
16
 let element: HTMLElement;
17
 let mockPaste: Pastebin[];
18
19
 // beforeEach is called once before every `it` block in a test.
20
 // Use this to configure to the component, inject services etc.
21
 
22
 beforeEach(()=> {
23
 
24
 TestBed.configureTestingModule({
25
 declarations: [ PastebinComponent ], // declare the test component
26
 imports: [ HttpModule], 
27
 });
28
 fixture = TestBed.createComponent(PastebinComponent);
29
 comp = fixture.componentInstance;
30
 de = fixture.debugElement.query(By.css('.pastebin'));
31
 element = de.nativeElement;
32
33
 });
34
})

There's a lot going on here. Let's break it up and take one piece at a time. Within thedescribeblock, we've declared some variables, and then we've used abeforeEachfunction. beforeEach() is a global function provided by Jasmine and, as the name suggests, itgets invoked once before every spec in thedescribeblock in which it is called.

1
TestBed.configureTestingModule({
2
 declarations: [ PastebinComponent ], // declare the test component
3
 imports: [ HttpModule], 
4
});

TestBedclass is a part of the Angular testing utilities, and it creates a testing module similar to that of the @NgModule class. Furthermore, you can configureTestBedusing theconfigureTestingModulemethod. For instance, you can create a test environment for your project that emulates the actual Angular application, and you can then pull a component from your application module and re-attach it to this test module.

1
 fixture = TestBed.createComponent(PastebinComponent);
2
 comp = fixture.componentInstance;
3
 de = fixture.debugElement.query(By.css('div'));
4
 element = de.nativeElement;

From the Angular documentation:

ThecreateComponentmethod returns aComponentFixture, a handle on the test environment surrounding the created component. The fixture provides access to the component instance itself and to theDebugElement, which is a handle on the component's DOM element.

As mentioned above, we've created afixture of thePastebinComponentand then used that fixture to create an instance of the component. We can now access the component's properties and methods inside our tests by calling comp.property_name.Since the fixture also provides access to thedebugElement, we can now querythe DOM elements and selectors.

There is an issue with our code that we haven't yet thought of. Our component has an external template and a CSS file. Fetching and reading them from the file system is an asynchronous activity, unlike the rest of the code, which is all synchronous.

Angular offers you afunction called async() that takes care of all the asynchronous stuff. What async does is keep track of all the asynchronous tasks inside it, while hiding the complexity of asynchronous execution from us. So we will now have two beforeEach functions, an asynchronous beforeEach() and a synchronous beforeEach().

1
/* pastebin.component.spec.ts */
2
3
 // beforeEach is called once before every `it` block in a test.
4
 // Use this to configure to the component, inject services etc.
5
 
6
 beforeEach(async(() => { //async before is used for compiling external templates which is any async activity
7
 TestBed.configureTestingModule({
8
 declarations: [ PastebinComponent ], // declare the test component
9
 imports: [ HttpModule], 
10
 })
11
 .compileComponents(); // compile template and css
12
}));
13
14
 beforeEach(()=> { //And here is the synchronous async function
15
 
16
 fixture = TestBed.createComponent(PastebinComponent);
17
 comp = fixture.componentInstance;
18
 de = fixture.debugElement.query(By.css('.pastebin'));
19
 element = de.nativeElement;
20
 });

We haven't written any test specs yet. However, it's a good idea to create an outline of the specs beforehand. The image below depicts a rough design of the Pastebin component.

Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (13)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (14)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (15)

We need to write tests with the following expectations.

  • The Pastebin component should exist.
  • Component's title property should be displayed in the template.
  • The template should have an HTMLtable to display the existing pastes.
  • pastebinService is injected into the component, and its methods are accessible.
  • No pastes should be displayed until onInit() is called.
  • Pastes are not displayed until after the promise in our Service is resolved.

The first three tests are easy to implement.

1
 it('should have a Component',()=> {
2
 expect(comp).toBeTruthy();
3
 });
4
5
 it('should have a title', () => {
6
 comp.title = 'Pastebin Application';
7
 fixture.detectChanges();
8
 expect(element.textContent).toContain(comp.title);
9
 })
10
 
11
 it('should have a table to display the pastes', () => {
12
 expect(element.innerHTML).toContain("thead");
13
 expect(element.innerHTML).toContain("tbody");
14
 })

In a testing environment, Angular doesn't automatically bind the component's properties with the template elements. You have to explicitly call fixture.detectChanges() every time you want to bind a component property with the template. Running the test should give you an error because we haven't yet declared the title property inside our component.

1
 title: string = "Pastebin Application";

Don't forget to update the template with a basic table structure.

1
<div class = "pastebin">
2
 <h2> {{title}}</h2>
3
 <table id="table" class="table table-hover table-striped">
4
<thead>
5
 <tr>
6
 <th> id </th>
7
 <th> Title </th>
8
 <th> Language </th>
9
 <th> Code </th>
10
 </tr>
11
</thead>
12
 
13
<tbody>
14
</tbody>
15
 
16
 </table>
17
</div>

As for the rest of the cases, we need to inject the Pastebinservice and write tests that deal with component-service interaction.A real service might make calls to a remote server, and injecting it in its raw form will be a laborious and challenging task.

Instead, we should write tests that focus on whether the component interacts with the service as expected. We shall add specs that spy on the pastebinService and its getPastebin() method.

First, import the PastebinService into our test suite.

1
import { PastebinService } from '../pastebin.service';

Next, add it to the providers array inside TestBed.configureTestingModule().

1
TestBed.configureTestingModule({
2
 declarations:[ CreateSnippetComponent],
3
 providers: [ PastebinService ],
4
});

The code below creates a Jasmine spy that is designed to track all calls to thegetPastebin()method and return a promise that immediately resolves to mockPaste.

1
//The real PastebinService is injected into the component
2
let pastebinService = fixture.debugElement.injector.get(PastebinService);
3
 mockPaste = [
4
 { id:1, title: "Hello world", language: "Ruby", paste: "puts 'Hello'" }];
5
 
6
 spy = spyOn(pastebinService, 'getPastebin')
7
 .and.returnValue(Promise.resolve(mockPaste));

The spy isn't concerned about the implementation details of the real service, but instead, bypasses any call to the actual getPastebin() method. Moreover, all theremote calls buried inside getPastebin() are ignored by our tests. We will be writing isolated unit-tests for Angular services in the second part of the tutorial.

Add the following tests to pastebin.component.spec.ts.

1
 it('should not show the pastebin before OnInit', () => {
2
 this.tbody = element.querySelector("tbody");
3
 
4
 //Try this without the 'replace(\s\s+/g,'')' method and see what happens
5
 expect(this.tbody.innerText.replace(/\s\s+/g, '')).toBe("", "tbody should be empty");
6
 expect(spy.calls.any()).toBe(false, "Spy shouldn't be yet called");
7
 });
8
9
 it('should still not show pastebin after component initialized', () => {
10
 fixture.detectChanges();
11
 // getPastebin service is async, but the test is not.
12
 expect(this.tbody.innerText.replace(/\s\s+/g, '')).toBe("", 'tbody should still be empty');
13
 expect(spy.calls.any()).toBe(true, 'getPastebin should be called');
14
 });
15
16
 it('should show the pastebin after getPastebin promise resolves', async() => {
17
 fixture.detectChanges();
18
 fixture.whenStable().then( () => {
19
 fixture.detectChanges();
20
 expect(comp.pastebin).toEqual(jasmine.objectContaining(mockPaste));
21
 expect(element.innerText.replace(/\s\s+/g, ' ')).toContain(mockPaste[0].title);
22
 });
23
 })

The first two tests are synchronous tests. The first spec checks whether the innerText of the div element stays empty as long as the component isn't initialized. The second argument to Jasmine's matcher function is optional and is displayed when the test fails. This is helpful when you have multiple expect statements inside a spec.

In the second spec,the component is initialized (because fixture.detectChanges() is called), and the spy is also expected to get invoked, but the template should not be updated. Even though the spy returns a resolved promise, the mockPaste isn't available yet. It shouldn't be available unless the test is an asynchronous test.

The third test uses anasync() function discussed earlier to run the test in an async test zone. async() is used to make a synchronous test asynchronous.fixture.whenStable() is called when all pending asynchronous activities are complemented, and then a second round of fixture.detectChanges() is called to update the DOM with the new values. The expectation in the final test ensures that our DOM is updated with the mockPaste values.

To get the tests to pass, we need to update our pastebin.component.ts with the following code.

1
/*pastebin.component.ts*/
2
3
import { Component, OnInit } from '@angular/core';
4
import { Pastebin } from '../pastebin';
5
import { PastebinService } from '../pastebin.service';
6
7
@Component({
8
 selector: 'app-pastebin',
9
 templateUrl: './pastebin.component.html',
10
 styleUrls: ['./pastebin.component.css']
11
})
12
export class PastebinComponent implements OnInit {
13
14
 title: string = "Pastebin Application";
15
 pastebin: any = [];
16
17
 constructor(public pastebinServ: PastebinService) { }
18
19
20
 //loadPastebin() is called on init
21
 ngOnInit() {
22
 this.loadPastebin();
23
 }
24
25
 public loadPastebin() {
26
 //invokes pastebin service's getPastebin() method and stores the response in `pastebin` property
27
 this.pastebinServ.getPastebin().then(pastebin => this.pastebin = pastebin);
28
 
29
 }
30
}

The template also needs to be updated.

1
<!--- pastebin.component.html -->
2
<div class = "pastebin">
3
 <h2> {{title}}</h2>
4
<table id="table" class="table table-hover table-striped">
5
 <thead>
6
<tr>
7
<th> id </th>
8
<th> Title </th>
9
<th> Language </th>
10
<th> Code </th>
11
</tr>
12
 </thead>
13
 <tbody>
14
<tr *ngFor="let paste of pastebin">
15
<td> {{paste.id}} </td>
16
<td> {{paste.title}} </td>
17
<td> {{paste.language}} </td>
18
<td> View code </td>
19
20
</tr>
21
 </tbody>
22
 <!--- <app-add-paste (addPasteSuccess)= 'onAddPaste($event)'> </app-add-paste> -->
23
</table>
24
</div>

Adding a New Paste

Generate an AddPaste component using Angular-CLI. The image below depicts the design of the AddPaste component.

Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (16)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (17)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (18)Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (19)

The component's logic should pass the following specs.

  • AddPaste component's templateshould have a button called Create Paste.
  • Clicking the Create Paste button should display a modal box with id 'source-modal'.
  • The click action should also update the component's showModal property to true. (showModalis a boolean property that turns true when the modal is displayed and false when the modal is closed.)
  • Pressing the save button should invoke the Pastebin service'saddPaste() method.
  • Clicking the close button should remove the id 'source-modal' from the DOM and update the showModal property to false.

We've worked out the first three tests for you. See if you can make the tests pass on your own.

1
describe('AddPasteComponent', () => {
2
 
3
 let component: AddPasteComponent;
4
 let fixture: ComponentFixture<AddPasteComponent>;
5
 let de: DebugElement;
6
 let element: HTMLElement;
7
 let spy: jasmine.Spy;
8
 let pastebinService: PastebinService;
9
10
 beforeEach(async(() => {
11
 TestBed.configureTestingModule({
12
 declarations: [ AddPasteComponent ],
13
 imports: [ HttpModule, FormsModule ],
14
 providers: [ PastebinService ],
15
 })
16
 .compileComponents();
17
 }));
18
19
 beforeEach(() => {
20
 //initialization 
21
 fixture = TestBed.createComponent(AddPasteComponent);
22
 pastebinService = fixture.debugElement.injector.get(PastebinService);
23
 component = fixture.componentInstance;
24
 de = fixture.debugElement.query(By.css('.add-paste'));
25
 element = de.nativeElement;
26
 spy = spyOn(pastebinService, 'addPaste').and.callThrough();
27
 //ask fixture to detect changes
28
 fixture.detectChanges();
29
 });
30
31
 it('should be created', () => {
32
 expect(component).toBeTruthy();
33
 });
34
35
 it('should display the `create Paste` button', () => {
36
 //There should a create button in the template
37
 expect(element.innerText).toContain("create Paste");
38
 });
39
40
 it('should not display the modal unless the button is clicked', () => {
41
 //source-model is an id for the modal. It shouldn't show up unless create button is clicked
42
 expect(element.innerHTML).not.toContain("source-modal");
43
 })
44
45
 it("should display the modal when 'create Paste' is clicked", () => {
46
 let createPasteButton = fixture.debugElement.query(By.css("button"));
47
 //triggerEventHandler simulates a click event on the button object
48
 createPasteButton.triggerEventHandler("click",null);
49
 fixture.detectChanges();
50
 expect(element.innerHTML).toContain("source-modal");
51
 expect(component.showModal).toBeTruthy("showModal should be true");
52
 })
53
})

DebugElement.triggerEventHandler() is the only thing new here. It is used to trigger a clickevent on the button element on which it is called. The second parameter is the event object, and we've left it empty since the component's click() doesn't expect one.

Summary

That's it for the day. In this first article, we learned:

  • how to setup and configure Jasmine and Karma
  • how to write basic tests for classes
  • how to design and write unit tests for components
  • how to create a basic service
  • how to use Angular testing utilities in our project

In the next tutorial, we'll create new components, write more tests components with inputs and outputs, services, and routes. Stay tuned for the second part of the series. Share your thoughts through the comments.

Testing Components in Angular Using Jasmine: Part 1 | Envato Tuts+ (2024)

FAQs

How Angular component is tested using Jasmine test framework? ›

Set up testinglink

The Angular CLI downloads and installs everything you need to test an Angular application with Jasmine testing framework. The ng test command builds the application in watch mode, and launches the Karma test runner. The last line of the log shows that Karma ran three tests that all passed.

How do you test a component in Jasmine? ›

We use the Jasmine-provided it function to define a spec. The first parameter of it is a text description of what the spec will be testing — in this case we have a defined component. The second parameter is a function that will run the test. We then use Jasmine's expect function to define our expectation.

How to test a particular component in Angular? ›

To run your tests using the Angular CLI, you use the ng test command in your terminal. As a result, Karma will open up the default browser and run all the tests written with the aid of Jasmine and will display the outcome of those tests.

How to use Jasmine with Angular? ›

A Simple Test Example
  1. // A Jasmine suite describe('Adder', () => { });
  2. function add(first, second) { if (true) { // why? if (true) { // why?? if (1 === 1) { // why?!?1 return first + second; } } } }
  3. ng new angular-testing --routing.
  4. cd angular-testing ng serve -o.
  5. ng test.
  6. ng g component home.
Sep 21, 2020

How to debug test cases in Angular Jasmine? ›

Debugging testslink
  1. Reveal the Karma browser window. ...
  2. Click the DEBUG button to open a new browser tab and re-run the tests.
  3. Open the browser's Developer Tools. ...
  4. Pick the Sources section.
  5. Press Control/Command-P , and then start typing the name of your test file to open it.
  6. Set a breakpoint in the test.

How does Jasmine testing work? ›

Jasmine follows Behavior Driven Development (BDD) procedure to ensure that each line of JavaScript statement is properly unit tested. By following BDD procedure, Jasmine provides a small syntax to test the smallest unit of the entire application instead of testing it as a whole.

What is Jasmine in Angular testing? ›

What is Jasmine? Jasmine is a free as well as an open-source Behavior Driven Development (BDD) framework that tests JavaScript code and also goes well with Karma. Like Karma, it is also the suggested testing framework within the Angular documentation.

How to mock a component in Angular unit testing? ›

A mock component in Angular tests can be created by MockComponent function. The mock component respects the interface of its original component, but all its methods are dummies. To create a mock component, simply pass its class into MockComponent function.

How do I run just one test on Jasmine? ›

Running a single spec

By using fit (think of it as a focused it ), Jasmine will run only that particular spec. describe('Awesome feature', function () { fit('should check whether `true` is really `true`', function () { expect(true). toBe(true); }); });

Which types of testing do you know Angular? ›

Type of Testing in Angular

There are two main types of Angular testing: unit testing and end-to-end testing. Unit testing is verifying that individual units of code behave as expected. End-to-end testing is verifying that the entire application behaves as expected.

How to run a particular component in Angular? ›

Using a component in Angular 8:
  1. Go to the component. html file and write the necessary HTML code.
  2. Go to the component. css file and write the necessary CSS code.
  3. Write the corresponding code in component. ts file.
  4. Run the Angular app using ng serve –open.
Nov 4, 2020

How do you mock an object in Jasmine? ›

To use mocks and spies in jasmine, you can use the jasmine. createSpy, jasmine. createSpyObj, and spyOn functions. These functions allow you to create mocks and spies for functions, objects, or methods, and configure their behavior and expectations.

How to mock a function in Angular Jasmine? ›

To mock a private function with Jasmine, we can spy on our service private function searchDoggos and use a fake callback, callFake , to provide the mocked data as return when needed. Moreover, we can also test that our function has effectively been executed. }); });

What is Jasmine tool used for? ›

Jasmine is an open-source testing framework for JavaScript. It aims to run on any JavaScript-enabled platform, to not intrude on the application nor the IDE, and to have easy-to-read syntax. It is heavily influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.

How to test Angular application using selenium? ›

Solution: Use JavascriptExecutor library
  1. Example of executeAsyncScript:
  2. Syntax:
  3. Creating Tests Using ngWebDriver.
  4. Maven Dependency XML Code.
  5. Creating Locators Using Angular Attributes.
  6. Creating Page Object Factories using ngWebDriver Locators.

How to do unit testing in Angular using Karma Jasmine? ›

An environment to run angular tests is being created using all the imports at the beginning of the file. TestBed is a powerful unit testing tool provided by angular, and it is initialized in this file. Finally, karma loads all the test files of the application matching their names against a regular expression.

How to test Angular test cases? ›

How to write a Unit Test in Angular?
  1. describe() – It's a suite of Test scripts that calls a global Jasmine function with two parameters: a string and a function. ...
  2. it() – It's the smallest unit test case that is written to be executed, which calls a global Jasmine function with two parameters: a string and a function.
Apr 21, 2023

Does Angular use jest or Jasmine? ›

Jasmine works best with Angular, much like Jest is most compatible with React. Any Angular app can use Jasmine to test its codebase—we simply use the Angular CLI to run ng test , which initiates the Karma test runner and outputs the results.

References

Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6515

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.