About Me ● Rainer Hahnekamp ANGULARarchitects.io ● Trainings and Consultancy ● > 20 Years Experience @rainerhahnekamp https://angulararchitects.io/workshops
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?
Agenda 1. Demo Application 2. Different Testing Techniques 3. Unit vs. Integration tests 4. Nx Architecture a. + DDD Extension by AngularArchitects 5. Testable Architecture
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
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
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?
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
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
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
Logical Groups Container Components Presentational Components State Management Domain Models Selected Mocking without DOM Selected Mocking with DOM interaction
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
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
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