Loading
Current section: Globals 5 exercises
Problem

Global values

Loading exercise

Transcript

00:00 Spying on globals is extremely powerful, but it only works on methods, so if you want to mock a global object, whether partially or completely, you would have to use a different technique. Like in this exercise, where your task would be to test this function called toAbsoluteUrl. The purpose of this function is quite straightforward.

00:18 It accepts the URL, which can be an absolute URL or relative path, and it always returns an absolute URL. In other words, if the URL you provided was already an absolute URL, it will return it as is. But if you provided a relative path, it will resolve it against the current location.

00:35 And because this utility depends on the global location object, we can safely say that it's meant to be run in the browser. In order for it to run in VTest, we are using a custom test environment called HappyDOM. This environment acts like a browser-like environment in Node.js, similar to GSDOM. And it's going to provide us the location object, which is going to be localhost at

00:54 port 3000 by default. But in your tests, in the assertions in your tests, you would have to write the entire absolute URL, including hostname and port. And you want to be in control of those. So if your colleague updates VTest configuration tomorrow, your tests don't suddenly start failing. The golden rule of assertion still holds true here.

01:14 To do that, you would have to spy on the location object in tests. So complete the existing test suits, and bear in mind that the location object looks extremely similar to a URL instance.