Slide 1

Slide 1 text

Angular Testing Ensure the reliability of your Angular applications with robust unit and integration testing strategies using testing tools.

Slide 2

Slide 2 text

https://honte.ch Blog: https://tim-honermann.com Senior Software Engineer & Consultant Angular & TypeScript Trainer Tim Honermann

Slide 3

Slide 3 text

fabiangosebrink.bsky.social Fabian Gosebrink GDE for Angular, Microsoft MVP, Nx Champion

Slide 4

Slide 4 text

Types of testing

Slide 5

Slide 5 text

Types of testing Unit Testing

Slide 6

Slide 6 text

Types of testing Unit Testing Shallow Testing

Slide 7

Slide 7 text

Types of testing Unit Testing Shallow Testing Integration Testing

Slide 8

Slide 8 text

Types of testing Unit Testing Shallow Testing Integration Testing End to End Testing

Slide 9

Slide 9 text

Testing Strategies

Slide 10

Slide 10 text

Testing Strategies Smallest as possible

Slide 11

Slide 11 text

Testing Strategies Smallest as possible Readable

Slide 12

Slide 12 text

Testing Strategies Smallest as possible Readable Mocking friendly

Slide 13

Slide 13 text

Testing Strategies Smallest as possible Readable Mocking friendly Test driven

Slide 14

Slide 14 text

Testing Tools

Slide 15

Slide 15 text

describe(...) // summarizes multiple tests test(...) // declares a test it(...) // declares a test beforeEach(...) // runs before every test afterEach(...) // runs after every test Testing Syntax

Slide 16

Slide 16 text

describe('1st tests', () => { it('true is true', () => { expect(true).toBe(true); }); }); Testing Syntax

Slide 17

Slide 17 text

import { FilterPipe } from './filter.pipe'; describe('FilterPipe', () => { let filterPipe: FilterPipe; // synchronous beforeEach beforeEach(() => { filterPipe = new FilterPipe(); }); it('filterPipe should be instanciated', () => { // ... }); it('filterPipe should filter', () => { // ... }); }); Testing Example - Pipe

Slide 18

Slide 18 text

Arrange Act Assert

Slide 19

Slide 19 text

expect(…).toBe(…); // Assert // Matcher toBe toBeDefined toBeTruthy toEqual toMatch toContain toThrow ... Testing Assertions

Slide 20

Slide 20 text

const mySpy = jest.spyOn(service, 'getAllFood') .mockReturnValue(...); comp.updateFood(); expect(mySpy).toHaveBeenCalledTimes(1); Spies Testing with Spies

Slide 21

Slide 21 text

beforeEach(async(() => { await TestBed.configureTestingModule({ imports: [HomeComponent], providers: [ { provide: FoodDataService, useClass: FoodServiceMock } ] }).compileComponents(); })); Testing Example - Testbed

Slide 22

Slide 22 text

import { MockProvider } from 'ng-mocks'; beforeEach(async(() => { await TestBed.configureTestingModule({ imports: [HomeComponent], providers: [ MockProvider(FoodDataService) ] }).compileComponents(); })); 1 2 3 4 5 6 7 8 9 10 Testing Example - Testbed

Slide 23

Slide 23 text

import { MockProvider } from 'ng-mocks'; beforeEach(async(() => { await TestBed.configureTestingModule({ imports: [HomeComponent], providers: [ MockProvider(FoodDataService) ] }).compileComponents(); })); 1 2 3 4 5 6 7 8 9 10 import { MockProvider } from 'ng-mocks'; 1 2 beforeEach(async(() => { 3 await TestBed.configureTestingModule({ 4 imports: [HomeComponent], 5 providers: [ 6 MockProvider(FoodDataService) 7 ] 8 }).compileComponents(); 9 })); 10 Testing Example - Testbed

Slide 24

Slide 24 text

import { MockProvider } from 'ng-mocks'; beforeEach(async(() => { await TestBed.configureTestingModule({ imports: [HomeComponent], providers: [ MockProvider(FoodDataService) ] }).compileComponents(); })); 1 2 3 4 5 6 7 8 9 10 import { MockProvider } from 'ng-mocks'; 1 2 beforeEach(async(() => { 3 await TestBed.configureTestingModule({ 4 imports: [HomeComponent], 5 providers: [ 6 MockProvider(FoodDataService) 7 ] 8 }).compileComponents(); 9 })); 10 MockProvider(FoodDataService) import { MockProvider } from 'ng-mocks'; 1 2 beforeEach(async(() => { 3 await TestBed.configureTestingModule({ 4 imports: [HomeComponent], 5 providers: [ 6 7 ] 8 }).compileComponents(); 9 })); 10 Testing Example - Testbed

Slide 25

Slide 25 text

import { MockProvider } from 'ng-mocks'; beforeEach(async(() => { await TestBed.configureTestingModule({ imports: [HomeComponent], providers: [ MockProvider(FoodDataService) ] }).compileComponents(); })); 1 2 3 4 5 6 7 8 9 10 import { MockProvider } from 'ng-mocks'; 1 2 beforeEach(async(() => { 3 await TestBed.configureTestingModule({ 4 imports: [HomeComponent], 5 providers: [ 6 MockProvider(FoodDataService) 7 ] 8 }).compileComponents(); 9 })); 10 MockProvider(FoodDataService) import { MockProvider } from 'ng-mocks'; 1 2 beforeEach(async(() => { 3 await TestBed.configureTestingModule({ 4 imports: [HomeComponent], 5 providers: [ 6 7 ] 8 }).compileComponents(); 9 })); 10 beforeEach(async(() => { await TestBed.configureTestingModule({ }).compileComponents(); import { MockProvider } from 'ng-mocks'; 1 2 3 4 imports: [HomeComponent], 5 providers: [ 6 MockProvider(FoodDataService) 7 ] 8 9 })); 10 Testing Example - Testbed

Slide 26

Slide 26 text

import { MockProvider } from 'ng-mocks'; beforeEach(async(() => { await TestBed.configureTestingModule({ imports: [HomeComponent], providers: [ MockProvider(FoodDataService) ] }).compileComponents(); })); 1 2 3 4 5 6 7 8 9 10 import { MockProvider } from 'ng-mocks'; 1 2 beforeEach(async(() => { 3 await TestBed.configureTestingModule({ 4 imports: [HomeComponent], 5 providers: [ 6 MockProvider(FoodDataService) 7 ] 8 }).compileComponents(); 9 })); 10 MockProvider(FoodDataService) import { MockProvider } from 'ng-mocks'; 1 2 beforeEach(async(() => { 3 await TestBed.configureTestingModule({ 4 imports: [HomeComponent], 5 providers: [ 6 7 ] 8 }).compileComponents(); 9 })); 10 beforeEach(async(() => { await TestBed.configureTestingModule({ }).compileComponents(); import { MockProvider } from 'ng-mocks'; 1 2 3 4 imports: [HomeComponent], 5 providers: [ 6 MockProvider(FoodDataService) 7 ] 8 9 })); 10 import { MockProvider } from 'ng-mocks'; beforeEach(async(() => { await TestBed.configureTestingModule({ imports: [HomeComponent], providers: [ MockProvider(FoodDataService) ] }).compileComponents(); })); 1 2 3 4 5 6 7 8 9 10 Testing Example - Testbed

Slide 27

Slide 27 text

it('should load todos', () => { // arrange const response = [ /* ...*/ ]; const getSpy = jest .spyOn(httpClient, 'get') .mockReturnValue(of(response)); // act const result$ = service.loadTodos(); // assert result$.subscribe((result) => { expect(result).toEqual(response); }); }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Testing Example - Async

Slide 28

Slide 28 text

it('should load todos', waitForAsync(() => { // arrange const response = [ /* ...*/ ]; const getSpy = jest .spyOn(httpClient, 'get') .mockReturnValue(of(response)); // act const result$ = service.loadTodos(); // assert result$.subscribe((result) => { expect(result).toEqual(response); }); })); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Testing Example - Async

Slide 29

Slide 29 text

it('should load todos', waitForAsync(() => { // arrange const response = [ /* ...*/ ]; const getSpy = jest .spyOn(httpClient, 'get') .mockReturnValue(of(response)); // act const result$ = service.loadTodos(); // assert result$.subscribe((result) => { expect(result).toEqual(response); }); })); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 it('should load todos', waitForAsync(() => { })); 1 // arrange 2 const response = [ /* ...*/ ]; 3 const getSpy = jest 4 .spyOn(httpClient, 'get') 5 .mockReturnValue(of(response)); 6 7 // act 8 const result$ = service.loadTodos(); 9 10 // assert 11 result$.subscribe((result) => { 12 expect(result).toEqual(response); 13 }); 14 15 Testing Example - Async

Slide 30

Slide 30 text

it('should load todos', waitForAsync(() => { // arrange const response = [ /* ...*/ ]; const getSpy = jest .spyOn(httpClient, 'get') .mockReturnValue(of(response)); // act const result$ = service.loadTodos(); // assert result$.subscribe((result) => { expect(result).toEqual(response); }); })); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 it('should load todos', waitForAsync(() => { })); 1 // arrange 2 const response = [ /* ...*/ ]; 3 const getSpy = jest 4 .spyOn(httpClient, 'get') 5 .mockReturnValue(of(response)); 6 7 // act 8 const result$ = service.loadTodos(); 9 10 // assert 11 result$.subscribe((result) => { 12 expect(result).toEqual(response); 13 }); 14 15 it('should load todos', waitForAsync(() => { // arrange const response = [ /* ...*/ ]; const getSpy = jest .spyOn(httpClient, 'get') .mockReturnValue(of(response)); // act const result$ = service.loadTodos(); // assert result$.subscribe((result) => { expect(result).toEqual(response); }); })); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Testing Example - Async

Slide 31

Slide 31 text

it('should load todos', fakeAsync(() => { // arrange // ... // act // ... tick(5000); // assert // ... })); 1 2 3 4 5 6 7 8 9 10 11 Testing Example - Async

Slide 32

Slide 32 text

it('should load todos', fakeAsync(() => { // arrange // ... // act // ... tick(5000); // assert // ... })); 1 2 3 4 5 6 7 8 9 10 11 it('should load todos', fakeAsync(() => { tick(5000); 1 // arrange 2 // ... 3 4 // act 5 // ... 6 7 8 // assert 9 // ... 10 })); 11 Testing Example - Async

Slide 33

Slide 33 text

it('should load todos', fakeAsync(() => { // arrange // ... // act // ... tick(5000); // assert // ... })); 1 2 3 4 5 6 7 8 9 10 11 it('should load todos', fakeAsync(() => { tick(5000); 1 // arrange 2 // ... 3 4 // act 5 // ... 6 7 8 // assert 9 // ... 10 })); 11 it('should load todos', fakeAsync(() => { // arrange // ... // act // ... tick(5000); // assert // ... })); 1 2 3 4 5 6 7 8 9 10 11 Testing Example - Async

Slide 34

Slide 34 text

Demo & Tasks Lets code !