How do you test route guards, pipes and directives?

Angular testing, tooling and architecture · Freshers and experienced

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