How do you test route guards, pipes and directives?
Model answer
A pipe is a class with a transform method, so test it directly with different inputs. A functional guard can be run inside TestBed.runInInjectionContext with fake services, and you check the returned value or UrlTree. A directive is tested by creating a small host component that uses it and checking the DOM and events.
Small focused tests give clear failures and run fast.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- How do you write a unit test for a component or a service in Angular?
- What is the difference between unit, integration and end-to-end tests?
- How do you test a component that calls an API?
- What does ng lint and a formatter give you, and what other tooling do Angular teams use?
- How do you handle environment-specific settings in Angular?
- How would you structure a large Angular application?