Slide 1

Slide 1 text

Testable Architecture in Angular Reliable Web Summit, 26. - 27. August 2021

Slide 2

Slide 2 text

About Me ● Rainer Hahnekamp ANGULARarchitects.io ● Trainings and Consultancy ● > 20 Years Experience @rainerhahnekamp https://angulararchitects.io/workshops

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Common Questions in Testing ● What should we test? ● How much should we test? ● Should we use the TestBed all the time? ● What components/services should be included in a test? ● What should we mock?

Slide 6

Slide 6 text

Overwhelmed by too many choices?

Slide 7

Slide 7 text

Architecture as a Guideline

Slide 8

Slide 8 text

Agenda 1. Demo Application 2. Different Testing Techniques 3. Unit vs. Integration tests 4. Nx Architecture a. + DDD Extension by AngularArchitects 5. Testable Architecture

Slide 9

Slide 9 text

Example Application

Slide 10

Slide 10 text

Dep-Graph Holidays Address Getter Brochure Sender Store Nominatim Validator validate- address.ts holiday. reducer.ts holiday. actions.ts Holiday Effects holiday. selectors.ts

Slide 11

Slide 11 text

Different Testing Techniques

Slide 12

Slide 12 text

Unit Tests Integration Tests End-to-End (E2E) Tests

Slide 13

Slide 13 text

1. Unit / Integration Range a. Full mocking, no TestBed b. Selected mocking, without DOM interaction c. Selected mocking, DOM interaction d. Most minimal mocking, DOM Interaction 2. Exotic a. RxJs Marbles b. Visual Regression c. Component Tests via Storybook/Cypress (E2E) Different Testing Techniques

Slide 14

Slide 14 text

Unit vs. Integration Tests

Slide 15

Slide 15 text

Unit Tests Integration Tests End-to-End (E2E) Tests

Slide 16

Slide 16 text

Testing Pyramid Revisited Unit Tests Integration Tests End-to-End Tests

Slide 17

Slide 17 text

Unit vs Integration Test Unit Tests ● Mock a lot ○ Very tightly coupled to implementation ● Advantages ○ Great code quality (FP) ○ Perfect fit for edge cases / finding bugs ○ Fast ● Disadvantages ○ Lots of code for mocking ○ Refactoring not really possible Integration Tests ● Mock out-of-system dependencies ○ Run against an API (UI) ● Advantages ○ Great for refactoring ○ Efficient (coverage) ● Disadvantages ○ Big setup required ○ Slow

Slide 18

Slide 18 text

Testing According to ROI Kent C. Dodds: https://twitter.com/kentcdodds/status/960723172591992832

Slide 19

Slide 19 text

Testing Pyramid Revisited Unit Tests Integration Tests End-to-End Tests Sweet Spot Edge Cases Modules

Slide 20

Slide 20 text

Potential Problems ● Unit Tests ○ What technique should be applied? ○ Too much mocking ○ Do I need unit tests for everything? ■ AngularCLI generated spec files ● Integration Tests ○ Too much setup required → feels like E2E ○ What should I mock / What is my entry point?

Slide 21

Slide 21 text

nx* Architecture * DDD Addition by AngularArchitects

Slide 22

Slide 22 text

Shared Forms Grid Error Handling Widgets Backend Middleware ... App Shell Domain Domain Domain Domain

Slide 23

Slide 23 text

Domain Feature (Container Cmp.) Data UI (Presentational Cmp.) Domain Models Component Service Module

Slide 24

Slide 24 text

Container Components ● Minimal HTML/CSS ● Lots of DI ● Facilitator ● HTML & CSS only ● Minimal Typescript ● No DI ● @Input & @Output ● No Observables @Input @Output Presentational Components Data (ngrx) selectors actions Container & Presentational Components

Slide 25

Slide 25 text

Dep-Graph Revisited Holidays Address Getter Store Nominatim Validator validate- address.ts holiday. reducer.ts holiday. actions.ts Holiday Effects holiday. selectors.ts Brochure Sender Holiday Card

Slide 26

Slide 26 text

Data Holiday Domain Feature Holidays Brochure Sender Store actions reducer effects selectors UI Holiday Card Shared Address Getter Nominatim Validator validate- address.ts

Slide 27

Slide 27 text

Testable Architecture ● Unit Tests ○ Class has a defined type ○ One testing technique per Type ● Integration Tests ○ Reduction of dependencies via domain/feature boundaries ○ Integration Test per Domain/Feature ○ Entry point is the feature component

Slide 28

Slide 28 text

Edge Cases Unit Tests Integration Tests Sweet Spot Edge Cases Modules

Slide 29

Slide 29 text

Edge Cases Examples ● Components ● Services ● Functions

Slide 30

Slide 30 text

1. Unit / Integration Range a. Full mocking, no TestBed b. Selected mocking, without DOM interaction c. Selected mocking, DOM interaction d. Most minimal mocking, DOM Interaction 2. Exotic a. RxJs Marbles b. Visual Regression c. Component Tests via Storybook/Cypress (E2E) Edge Cases: Testing Techniques

Slide 31

Slide 31 text

Edge Cases Container Components Presentational Components State Management Domain Models Visual Regression Snapshot Full Mocking / no TestBed RxJs Marbles

Slide 32

Slide 32 text

Modules Unit Tests Integration Tests Sweet Spot Edge Cases Modules

Slide 33

Slide 33 text

Modules Examples ● Interplay between Container & Presentational Components ● Complex Components (DataGrid) ● State Management

Slide 34

Slide 34 text

1. Unit / Integration Range a. Full mocking, no TestBed b. Selected mocking, without DOM interaction c. Selected mocking, DOM interaction d. Most minimal mocking, DOM Interaction 2. Exotic a. RxJs Marbles b. Visual Regression c. Component Tests via Storybook/Cypress (E2E) Modules: Testing Techniques

Slide 35

Slide 35 text

Logical Groups Container Components Presentational Components State Management Domain Models Selected Mocking without DOM Selected Mocking with DOM interaction

Slide 36

Slide 36 text

Sweet Spot Unit Tests Integration Tests Sweet Spot Edge Cases Modules

Slide 37

Slide 37 text

Sweet Spot Examples ● Per Domain ● Per Feature ● Tests which are too hard for E2E

Slide 38

Slide 38 text

1. Unit / Integration Range a. Full mocking, no TestBed b. Selected mocking, without DOM interaction c. Selected mocking, DOM interaction d. Most minimal mocking, DOM Interaction 2. Exotic a. RxJs Marbles b. Visual Regression c. Component Tests via Storybook/Cypress (E2E) Sweet Spot: Testing Techniques

Slide 39

Slide 39 text

Sweet Spot Container Components Presentational Components State Management Domain Models

Slide 40

Slide 40 text

Summary ● Try to go for Integration Tests ● Leave the edge cases for Unit Tests with full mocking ● Cover what's left with "Modules" Tests ● Apply the right techniques: ○ Visual Regression: Presentational Components ○ RxJs Marbles: ngRx Effect ○ Storybook/Cypress: Component Groups

Slide 41

Slide 41 text

Thank you very much https://github.com/rainerhahnekamp/testable-architecture-in-angular

Slide 42

Slide 42 text

Schedules: Public Testing Workshop ● 25. & 26. October (German) ● 28. & 29. October (English) ● 6. & 7. December (English) Cooperation with Angular Academy ● 15. September - 17. September ● 29. September - 1. October

Slide 43

Slide 43 text

Image Sources ● Babel: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Kunsthistorisches_Museum_Wien%2C_Pieter_Bruegel_d.%C3%84.%2C_der_Turmbau_zu_Babel.JPG/1024px-Ku nsthistorisches_Museum_Wien%2C_Pieter_Bruegel_d.%C3%84.%2C_der_Turmbau_zu_Babel.JPG ● Wiener Tafelspitz: https://www.flickr.com/photos/pokpok/382061 ● Wiener Schnitzel: https://pixabay.com/get/gcb51eeba20917a5033792cd225f69146ce3ca04e07ecdabefbe0014c3280c5237b7ac2050244c38855e83408e24c0b9f_1920.jpg ● Kaiserschmarrn: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/2015_0731_Kaiserschmarrn_Apfelso%C3%9Fe_Edelweissh%C3%BCtte_S%C3%B6lden.jpg/960px-2015_0731_Kai serschmarrn_Apfelso%C3%9Fe_Edelweissh%C3%BCtte_S%C3%B6lden.jpg ● Sacher Torte: https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Original_Sacher-Torte_%C2%A9Hotel_Sacher.jpg/1024px-Original_Sacher-Torte_%C2%A9Hotel_Sacher.jpg ● No Kangaroos in Austria: https://ih1.redbubble.net/image.467145234.1731/bg,f8f8f8-flat,750x,075,f-pad,750x1000,f8f8f8.u5.jpg ● Sound of Music Cover: https://www.rottentomatoes.com/m/sound_of_music