Instructor: 0:00 All right. Mocking. This is a big subject and people can find it to be pretty confusing. I'm excited to teach you about it. With this one, we're going to be taking Jest mock functionality and comparing it to how we would implement that in user land if we were just going to implement it in node. This will hopefully give you some good insight into how it works.
0:21 The general idea behind mocking is you've got this module that's doing something. You want to test it, but this module is using another module that's doing something that maybe it's sending a request to a credit card payment processing company. You don't have a big enough credit limit or something. You don't want to charge yourself when you're running your test, right?
0:40 We're going to mock out that module that's making the credit card payment processing request, so that it doesn't do that when you're running your test. Then you want to write some test around it to make sure that you're using that module properly. The idea behind mocking it can be a little bit confusing. Let's take a look at the project here.
1:01 Here, we're going to mostly be working in the /src directory. The thing that we're trying to test is this thumbWar module, which is taking a player1 and a player2 and then calling this getWinner on three games. Numbers to win is two, so best two out of three in determining who is going to be the winner of this game.
1:21 It's using this really sophisticated algorithm in this utils, which actually is not sophisticated at all. Just an example. The idea here is we want to mock out the getWinner function. There are various ways to do this and we start out by simply monkey-patching and then we become a little bit more sophisticated and advanced.
1:40 The way that we get this done is we have our Jest test in here and then we have our no framework test in here. We start out in the Jest test here and then we go to the no framework side and try to implement those same features that we have in Jest without a framework. You get an idea of how the framework is doing what it's doing.
2:01 The way that this is all working, it's just interesting is we have this other directory where we create a custom runner so that we run our code just a node without Jest installed. It's interesting to dive into, so feel free to inspect that and play around with that a little bit. Jest is a really powerful tool, so it allows us to do that. For our regular Jest test, those are just regular Jest configuration stuff going on here.
2:24 I hope you enjoyed this module. Even though what we're doing in node land without Jest is not what Jest is doing at all because Jest is in total control of the module system when it's running your test, it's enlightening to see how this might be working if we were to implement it ourselves.