Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular testing made easy with Jest and Cypress

Angular testing made easy with Jest and Cypress

Despite its importance, testing is often neglected in software development. In this workshop, Fabian Gosebrink will demonstrate how to make Angular testing easy using the powerful testing frameworks Jest and Cypress.

Participants will learn how to write effective tests for their Angular applications and ensure that they are thoroughly tested and reliable.

By the end of the workshop, participants will have a solid understanding of Angular testing best practices and be able to apply these skills to their own projects.

Fabian Gosebrink

March 19, 2025
Tweet

More Decks by Fabian Gosebrink

Other Decks in Technology

Transcript

  1. Angular Testing Ensure the reliability of your Angular applications with

    robust unit and integration testing strategies using testing tools.
  2. describe(...) // summarizes multiple tests test(...) // declares a test

    it(...) // declares a test beforeEach(...) // runs before every test afterEach(...) // runs after every test Testing Syntax
  3. describe('1st tests', () => { it('true is true', () =>

    { expect(true).toBe(true); }); }); Testing Syntax
  4. 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
  5. beforeEach(async(() => { await TestBed.configureTestingModule({ imports: [HomeComponent], providers: [ {

    provide: FoodDataService, useClass: FoodServiceMock } ] }).compileComponents(); })); Testing Example - Testbed
  6. 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
  7. 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
  8. 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
  9. 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
  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 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. it('should load todos', fakeAsync(() => { // arrange // ...

    // act // ... tick(5000); // assert // ... })); 1 2 3 4 5 6 7 8 9 10 11 Testing Example - Async
  16. 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
  17. 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