Check out a free preview of the full JavaScript Testing Practices and Principles course
The "Monkey Patch a Mock Exercise" Lesson is part of the full, JavaScript Testing Practices and Principles course featured in this preview video. Here's what you'd learn in this lesson:
After explaining mocking, Kent introduces the exercise where the goal is to create a mock for a dependency in a test. The simplest form of mocking a function in a module is by using monkey patching to swap the actual implementation with a fake implementation.
Transcript from the "Monkey Patch a Mock Exercise" Lesson
[00:00:03]
>> Kent C. Dodds: So our next subject is mocks and test doubles, spies, whatever you wanna call these things. A fake version of something. And so to teach you this, you're actually going to build your own, or you're gonna work through creating a mock for a dependency in a test, okay?
[00:00:28]
So this is also going to be in the other directory in whats-a-mock? And in here, let me really quick, I think I've got a script for this. Yeah, it's npm run test:mock.
>> Kent C. Dodds: And that should start your Jest up in Watch mode. If you're curious, these scripts all exist in the package.json here.
[00:00:57]
So if you're like, what was that script again? Just look through here, find the one that looks like the one we're working on. So it's test:mock.
>> Speaker 2: What directory do we need to be in?
>> Kent C. Dodds: Yeah, in this one, the directory, it should just be the root directory of the project.
[00:01:16]
>> Kent C. Dodds: So let me just intro you to this really quick. We've got a thumb-war module. So it takes a player1 and player2. Those are just accepted as strings. It goes through and calls this getWinner function from utils. And it keeps going until player1 or player2 win at least two, whoever gets that, wins.
[00:01:48]
So we're going to make this act under the assumption that getWinner is a module that makes some request to some unreliable service. Well, it's reliable for production, but its test environment is unreliable or something. In addition, when you're unit testing, you don't wanna be making any network requests anyway.
[00:02:08]
So, yeah, if you look at the actual implementation, it's just using Math.random. But let's just pretend, put our pretend hats on, that this is something really complicated we don't want to actually run in our test. So this is what we're gonna be mocking, this getWinner function from the utils module.
[00:02:26]
You wanna be in todo, whew. So, yeah, if you look at two here, we'll hit P and go thumb-war.1.todo. And you'll see that the test is still passing, but the problem is that the assertion isn't very specific, because it can't be. Because the winner is non-deterministic, you can't encode that in your test because you can't really know.
[00:02:52]
All that we know is that the winner's gonna be one of the two players. And so what we want to do is we're gonna mock this so we can have a more specific assertion. And we're gonna iterate from a less optimal mocking strategy to a more optimal mocking strategy.
[00:03:11]
So I've got comments. This is very similar to our building and testing framework that we did at the beginning. I've got comments in the code. I'll just kinda let you go on it and then we'll work through the, I'll work through the solution, at the end you can ask questions and everything.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops