Loading
Current section: Custom Assertions 3 exercises
lesson

Intro to Custom Assertions

Loading lesson

Transcript

00:00 So we've got a bunch of assert functions in our test, and I'm not a super fan of those. I like the actual assertions that are built in. Well, it turns out that we can turn these things into our own assert function. So if we had this example of assert logged in,

00:16 where we're looking for a link that says the user's name, and a link or a button that says log out, and that's how we determine that the user's logged in, then this is how you might implement that using the expect extend API that we get from vTest.

00:35 So you say expect extend, here's my to be logged in, and here's what we expect it to be, so here's the username. And then we look for those things, and we pass if there's a link in the button. Otherwise, we're gonna say expect to be logged in as username.

00:54 But I wish it was this easy. It's super duper not, because it can get really complicated, because expect can have a not added to it. And for that reason, and also there's resolve, like various other things that you have to think about. And also the formatting of the message as well.

01:13 So if you have is not, then now your message gets a little bit more tricky. You don't have to change the pass, vTest will do that, will manage that for you. It's just the message needs to be updated to handle that sort of situation. And then we've also got a cool utility for coloring things in a special way

01:32 so that they look like this is the expected thing, and this is the received thing. And so that can be handy, those utils. And then we've got this typing so that we can get autocomplete on expect. So when we say expect whatever dot,

01:50 and then we can get autocomplete for to be logged in, and even the type safety for this first value. Now, or that the value you pass in your assertion. The first value you don't get to type, that is just expect allows anything right there. But yeah, we get to type that value

02:09 in our type definitions right here, in our custom matchers. So with that, now I think you're ready to write your first custom matcher. And in this exercise, we're not gonna go super deep on that, because this could be an entire workshop on its own. It just gets really gnarly trying to figure out,

02:28 okay, we're in the is not situation, so we gotta deal with that. And so we're gonna let Kelly, the coworker, do like lots of the fine tuning, but I want you to be exposed to this because you probably will want to write a couple of your own custom matchers for your own apps. And it will take a little bit of practice to get used to the APIs.

02:49 So, but like if you want to do that practice now, you're more than welcome and you can compare your solution with Kelly's and that'll be awesome. But yeah, that's what we're gonna do in this exercise. I think you're gonna have a good time. We'll see you on the other side.