I'm upgrading my book series to Angular 11, and Angular 11 has a new strict typing mode. I had a few problems writing tests.

Let's look at this guard. In Angular a guard is a way to validate that user can have permission to access a route, or not:

view plain print about
1canActivate(
2 route: ActivatedRouteSnapshot,
3 state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
4 // does stuff that doesn't reference the route or state variables
5 return true;
6 }

Since this is an Angular specific method, I can't change the argument list, even though those arguments aren't used inside the method. I used to null these values out inside a test:

view plain print about
1const result = guard.canActivate(null, null);

And everything was great. Unfortunately, with Angular 11's new strict typing enabled I was not able to rely on the null value.

I could easily create a dumb instance of the ActivatedRouteSnapshot, but went down a rabbit hole trying to figure out how to mock or create an instance of the RouterStateSnapshot, because it's constructor requires an argument.

After some experimentation, I found an easier way:

view plain print about
1const result = guard.canActivate(new ActivatedRouteSnapshot(), {} as RouterStateSnapshot);

Instead of trying to create a mock or creating my own instance, I just cast an empty object as the RouterStatesnapshot. This passed compiler's tricks, and the tests ran without issue.

What have you been up to with Angular 11?