Transcript
00:00 When we express our expectations in tests, it often comes down to value comparison. We want to make sure that the actual state of the system that we're testing equals to the expected state. And to describe this, we often compare values. For example, making sure that the page URL equals to this string or a certain property of the user object is true.
00:18 There are multiple ways to compare values in VTest, using matchers like toBe, toEqual, toMatchObject, as well as asymmetric matchers like objectContaining or stringMatching. But it's important to keep in mind that with all of those matchers, VTest does structural comparison. So what if you have to compare things that are semantically equal, but differ structurally?
00:37 Like in this test case, where I compare different measurements. I have this measurement class to help me out, and it accepts two arguments, the value and the unit, like inches or centimeters. So when I'm comparing two measurements with the same value and unit, I expect this test to pass. But what happens when I compare one edge to 2.54 centimeters?
00:57 Well, in this case, the test will fail. Although we humans know that one inch equals to 2.54 centimeters, VTest doesn't know that. What it ends up comparing are the actual objects yielded by those classes, and they have different values of their properties, so naturally these objects are not the same on the structural level.
01:16 Well, you might be thinking, I can solve this by introducing a custom matcher, something like toEqualMeasurement. And this will work, but there is one detail to keep in mind. You would have to use this custom matcher explicitly. So if this measurement happened to be nested in a larger data structure, and you're just comparing it to another data, nothing will run your custom matcher.
01:35 So what you can do instead is teach VTest how to compare your custom values, basically tapping into the default behavior of the toEqualMatcher and extending it. And that is exactly your task for this exercise. I've already prepared a test case for you, so your job will be to teach VTest to compare values that are structurally different, but are the same semantically.
01:54 And you will do so by defining what's called a custom equality tester. So follow the instructions, and make sure that you have the test passing by the end. And once you're done, let's go through the solution together.
