1 00:00:00,240 --> 00:00:06,120 In this lesson we're going to continue to explore new testing techniques and the goal here is to figure 2 00:00:06,120 --> 00:00:14,820 out how we can mock our NPM modules so the process of marking is the process of replacing the real functions 3 00:00:14,820 --> 00:00:19,950 that run with functions that you create when we're running in a test environment. 4 00:00:19,950 --> 00:00:22,560 Now why might that be something we ever want to do. 5 00:00:22,800 --> 00:00:26,580 Well let's think about what all of this code actually does. 6 00:00:26,580 --> 00:00:32,270 We run various endpoints like the one for signing up and the one for deleting accounts. 7 00:00:32,400 --> 00:00:39,120 And we know that when both of these execute emails are sent behind the scenes emails are sent to email 8 00:00:39,150 --> 00:00:42,560 addresses that don't exist and will never be checked. 9 00:00:42,660 --> 00:00:45,510 And that alone might not be the worst thing. 10 00:00:45,510 --> 00:00:51,150 The problem though is that we're constantly rerunning our test cases which means we're just racking 11 00:00:51,150 --> 00:00:55,970 up unnecessary emails being sent from our set grid account. 12 00:00:55,980 --> 00:01:00,470 Now if we're on the free plan that means we don't have many free emails to send. 13 00:01:00,600 --> 00:01:06,540 And if we're on a paid plan and that means we're wasting money on emails that will never be opened with 14 00:01:06,540 --> 00:01:13,830 mocking we can make sure that when our program is running using just we don't actually send those emails 15 00:01:13,920 --> 00:01:14,550 off. 16 00:01:14,560 --> 00:01:19,130 Well let's get started by creating the directory where we can store our marks. 17 00:01:19,140 --> 00:01:25,530 So right inside of the test folder if we're trying to mock and NPM module what do we do we create a 18 00:01:25,530 --> 00:01:27,270 MOX directory. 19 00:01:27,270 --> 00:01:32,070 Now it starts with two underscores that's two there and not just one. 20 00:01:32,070 --> 00:01:39,900 Then we type out MOX in lower case than it's two more underscores this is the directory name that just 21 00:01:39,900 --> 00:01:47,220 is going to look for now in here we can create files for the individual modules we're trying to mock. 22 00:01:47,220 --> 00:01:52,050 So if I wanted to mock something like Jason Webb token I would create Jason Webb. 23 00:01:52,050 --> 00:02:00,120 Token dot J S in the MOX directory in this case we're trying to mark the module used right here that 24 00:02:00,120 --> 00:02:03,200 is at seven grid forward slash male. 25 00:02:03,270 --> 00:02:09,990 Now right here they are using at followed by the organization then forward slash the individual utility. 26 00:02:09,990 --> 00:02:16,290 This is known as NPM scope and all that means for us is that we're going to create a directory with 27 00:02:16,290 --> 00:02:18,520 this name inside of MOX. 28 00:02:18,630 --> 00:02:25,020 Then in that folder it will create a file with this name and the J.S. extension that will allow us to 29 00:02:25,020 --> 00:02:32,160 mark out the functionality providing our own versions of set API key and send to use when running in 30 00:02:32,160 --> 00:02:32,940 test mode. 31 00:02:33,390 --> 00:02:40,590 So right here let's get that done in the MOX folder will create a new folder at send a grid and in the 32 00:02:40,590 --> 00:02:46,380 send grid directory it will create a new file this one mail dot J S. 33 00:02:46,680 --> 00:02:52,590 Now it's important that when we're marking out a module we provide all of the things that that module 34 00:02:52,650 --> 00:02:54,420 needs in order to work. 35 00:02:54,450 --> 00:03:00,940 So our application code is expecting things like set API key and send to actually exist. 36 00:03:00,960 --> 00:03:06,510 So if we're going to mark that module out we need to provide our own versions of all of this. 37 00:03:06,510 --> 00:03:09,160 So the code at least doesn't fail. 38 00:03:09,180 --> 00:03:15,810 So right here that means we need to export an object with those properties on it because that's exactly 39 00:03:15,810 --> 00:03:17,340 what set grid is doing. 40 00:03:17,340 --> 00:03:20,820 We get an object back with those properties sitting right on it. 41 00:03:21,420 --> 00:03:28,650 So over here will actually kick things off by using module exports and setting it equal to a new empty 42 00:03:28,650 --> 00:03:29,640 object. 43 00:03:29,640 --> 00:03:36,210 And on here we'll provide a set API key and send no down below you'll actually notice that our test 44 00:03:36,210 --> 00:03:42,470 suite is failing and if we scroll up we can see why right here test suite failed to run. 45 00:03:42,510 --> 00:03:45,630 And that's referring to our user test suite. 46 00:03:45,630 --> 00:03:50,750 And the reason is that s g mail dot set API key is not a function. 47 00:03:50,850 --> 00:03:56,760 And they're right now the reason this is happening is because we've provided a mark for that module 48 00:03:56,970 --> 00:04:00,170 but we're not exporting an object with that function defined. 49 00:04:00,180 --> 00:04:03,230 So let's fix it right here on this object. 50 00:04:03,240 --> 00:04:07,640 I'm going to set a value for set API key. 51 00:04:07,710 --> 00:04:13,830 I'll go ahead and set it equal to a new function and I'll do the same thing down below for the other 52 00:04:13,830 --> 00:04:16,060 method we're using sent. 53 00:04:16,110 --> 00:04:21,750 Now if we were using even other methods from the module we would want to mark those as well. 54 00:04:21,750 --> 00:04:27,480 Now when it comes to mocking you could choose to accept arguments and then return some sort of fixed 55 00:04:27,480 --> 00:04:28,560 value. 56 00:04:28,560 --> 00:04:36,330 In this case in account dot J S we pass in arguments like the email to be sent or the API key to be 57 00:04:36,330 --> 00:04:36,770 set. 58 00:04:37,020 --> 00:04:40,020 But we don't do anything with the return value. 59 00:04:40,080 --> 00:04:44,670 So there's actually no need to do anything inside of these functions. 60 00:04:44,700 --> 00:04:50,760 All this is gonna do is allow these functions to be called over here but in reality nothing is happening 61 00:04:50,760 --> 00:04:54,170 behind the scenes and that's good in the test environment. 62 00:04:54,270 --> 00:05:02,880 No api keys will be set and no emails will get sent now if I go ahead and save male dot J S we'll see 63 00:05:02,880 --> 00:05:08,370 that our test suite does restart and this time it should be brought back to its working state. 64 00:05:08,370 --> 00:05:09,290 And it is. 65 00:05:09,360 --> 00:05:14,520 Now if you want to learn more about MOX as always it's defined in the documentation like everything 66 00:05:14,520 --> 00:05:19,270 else we have explored related to just under guides in the sidebar. 67 00:05:19,290 --> 00:05:24,960 There's a page called the manual marks which covers everything we just talked about right here a mocking 68 00:05:25,020 --> 00:05:28,560 note modules outlines what we just did. 69 00:05:28,560 --> 00:05:31,020 So that's where we're going to stop for this one. 70 00:05:31,020 --> 00:05:37,680 I wanted to introduce the concept of marking allowing us to not send those e-mails from our test environment 71 00:05:37,930 --> 00:05:41,680 though we won't be using them for anything else in this test suite. 72 00:05:41,730 --> 00:05:47,760 The nice thing now though is that as our test suite reruns we're not going to be wasting our limited 73 00:05:47,850 --> 00:05:50,070 emails that we can send off. 74 00:05:50,100 --> 00:05:50,750 All right. 75 00:05:50,760 --> 00:05:53,230 That's where we're going to stop for this one. 76 00:05:53,280 --> 00:05:54,770 I'm excited to continue on. 77 00:05:54,800 --> 00:05:56,760 So I'll see you in the next lesson.