Why can't Angular find the path() method on a Location object?

I'm writing some unit tests to test an Angular Router. In the test, I have code like this:

view plain print about
1expect(location.path()).toBe('/something');

IntelliJ is giving me errors, saying it can't find the path value:

Clearly the path() method exists on the Angular Location object, so what was the cause of the error?

Apparently there is a JavaScript Location object.

The browser Location object does not contain a path() function even though the Angular one does.

Since Location exists natively, IntelliJ did not import the Angular library, and I didn't notice until I tried to run the code.

The fix is pretty simple. Just import the Angular Location library manually:

view plain print about
1import {Location} from "@angular/common";

And you should be good to run your code and unit tests without any errors.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)

Comments are not allowed for this entry.