Loading
Current section: HTTP Mocking 5 exercises
lesson

Intro to Http Mocking

Loading lesson

Transcript

00:00 Alright, it's time to mock things again. We're going to be mocking HTTP, and you've already done this a couple of times with MSW. We're going to continue to use MSW for this, but there are a couple of considerations that we need to address in this test. So, again, the more your tests resemble the way your software is used, the more confidence they can give you.

00:18 That is 100% true. However, for being reasonable, we do have to fake things out in some situations. And so, like, if their system goes down, your test will be completely broken. During development, you're reliant on their progress if they're actively working on their end of the integration.

00:36 So, like, this is especially useful if you've got other teams in the company. They're working on their APIs. You have to wait until they're done. Like, no, no, we don't want to do that. And then you can't work offline if you rely on their system. Another thing we should add on there is if it costs money. Yeah, that's not good. Money bags right there.

00:54 So, yeah, there are various reasons where you want to mock the APIs that you're using. And luckily, because MSW is just so great, you can use the same mocks in your development as you can in your testing. And you get a lot of mileage out of that.

01:10 So, one of the things we're going to do is make sure that our mock server is running during testing. That'll be the first step. And then the next step we're going to do is handle one-off mocks. So, let's say you've got the happy path already mocked out, and you want to test the sad path.

01:26 Well, you don't want to adjust your happy path mock because, like, that would make it overly complex, and there's a level of indirection there. And so what you can do instead is you have your setup server where you're providing these mocks,

01:38 and then you use that server to, say, server.use, and you can override the mocks for a particular test. But then you need to make sure that you're resetting your handlers between tests so that you don't have a situation where the tests get affecting each other,

01:58 where the mock or the override that you set in one test is going to impact the other test. So, we're going to reset those handlers between those tests. And then finally, we're going to talk about setup functions. This is basically just like use JavaScript and functions, but there's something especially important about this that I want to talk with you about.

02:17 So, you've got a couple of steps on this exercise. I think you'll have a good time. Have fun.