1 00:00:01,670 --> 00:00:06,290 This video we're going to take a look at how to get just to essentially fake out this import statement 2 00:00:06,320 --> 00:00:07,550 or redirect it. 3 00:00:08,180 --> 00:00:09,290 Here's the entire process. 4 00:00:09,290 --> 00:00:11,260 This is what we're gonna do in this video. 5 00:00:11,370 --> 00:00:16,550 The first we're going to do is find the file that we want to make in our case the file that we want 6 00:00:16,550 --> 00:00:23,100 to fake is our nets wrapper dot t s file inside of our roots s our C directory. 7 00:00:23,100 --> 00:00:24,740 So this and that's wrapped right here. 8 00:00:24,740 --> 00:00:29,930 This has all the code that is going to try to actually connect to our real NAT server and we did not 9 00:00:29,930 --> 00:00:34,670 want to run this code because we don't want to have to run a copy of nets when we are executing our 10 00:00:34,670 --> 00:00:35,600 tests. 11 00:00:35,690 --> 00:00:41,040 So this is some code that we want to somehow fake out so to do so we're going to find that file. 12 00:00:41,130 --> 00:00:46,950 Well just found it then inside the same directory we're gonna create a folder called underscore underscore 13 00:00:47,130 --> 00:00:49,290 box underscore underscore. 14 00:00:49,380 --> 00:00:50,620 Let's do that right now. 15 00:00:51,420 --> 00:00:57,310 Go inside the same directory which in this case to be s RC I'll make a new folder called underscore 16 00:00:57,580 --> 00:01:02,530 underscore make sure you get two in their box underscore underscore 17 00:01:05,760 --> 00:01:10,260 after that we're going to open up that folder we've just created and we're gonna create a file inside 18 00:01:10,260 --> 00:01:13,500 there with identical names the file that we are trying to fake. 19 00:01:13,560 --> 00:01:18,570 So in our case we are trying to fake or kind of duplicate or redirect Nat's wrapper. 20 00:01:18,590 --> 00:01:19,310 Yes. 21 00:01:19,380 --> 00:01:23,910 So you'll make an identically named file inside of underscore underscore Max. 22 00:01:24,070 --> 00:01:26,310 So that's wrapper dot. 23 00:01:26,340 --> 00:01:34,140 Yes then inside this file we're going to write out some fake implementation so in our case this is where 24 00:01:34,140 --> 00:01:40,220 we're going to want to somehow fake out or provide some code to run instead of this Nats wrapper stuff 25 00:01:41,830 --> 00:01:48,890 so to do so inside of the new file we just created we can do an export onsite Nat's wrapper and I'm 26 00:01:48,890 --> 00:01:51,680 going to directly return an object. 27 00:01:51,800 --> 00:01:55,220 So why do I need to do a export continental wrapper. 28 00:01:55,220 --> 00:02:01,090 Well we are essentially trying to fake out or duplicate the functionality of the real Nats wrapper file. 29 00:02:01,250 --> 00:02:07,910 The real Nats wrapper file if we go down to the bottom it exports a single named export of Nats wrapper 30 00:02:08,420 --> 00:02:13,110 and that is going to be an instance of a class or essentially an object. 31 00:02:13,370 --> 00:02:18,980 So we can't build up or export Nats wrapper and then this is the object that we are going to export 32 00:02:19,400 --> 00:02:24,800 so we can write out our fake implementation or essentially some code to run instead of the real stuff 33 00:02:25,040 --> 00:02:27,040 inside of this object. 34 00:02:27,110 --> 00:02:32,480 Right now I'm going to leave this empty we'll come back and write in some fake implementation in just 35 00:02:32,480 --> 00:02:35,020 a moment. 36 00:02:35,030 --> 00:02:35,300 All right. 37 00:02:35,330 --> 00:02:40,420 Now the last step we're going to make sure that we tell just to use that fake file we just put together 38 00:02:40,630 --> 00:02:43,220 inside of our test file. 39 00:02:43,440 --> 00:02:47,940 Right now we have a couple of different files that are essentially trying to make use of this Nats wrapper 40 00:02:47,940 --> 00:02:48,660 stuff. 41 00:02:48,760 --> 00:02:56,170 They're specifically the new and update set of tests so inside of my test directory we're going to first 42 00:02:56,170 --> 00:02:57,990 focus on getting the new test. 43 00:02:58,000 --> 00:03:04,170 Yes file working correctly so inside of their I'm going to add in an import statement at the top of 44 00:03:04,170 --> 00:03:11,280 this new test file that's gonna tell just to make use of our mock file but to do so we'll write out 45 00:03:11,280 --> 00:03:18,220 just not Mark and then we'll write out a relative path to that file that we want to fake. 46 00:03:18,930 --> 00:03:23,970 So in this case the file we want to fake is Nat's wrapper inside the SRT RC directory that you write 47 00:03:23,970 --> 00:03:25,200 out a relative path to it. 48 00:03:25,230 --> 00:03:32,490 We would go up one directory from underscore underscored test into routes up another directory into 49 00:03:32,490 --> 00:03:35,720 us our C and then we can get Nat's dash wrapper. 50 00:03:35,730 --> 00:03:43,590 So in total up one directory of one directory that's dash wrapper that whenever these tests are executed 51 00:03:44,070 --> 00:03:47,070 Justin's going to see that we are trying to mock that file. 52 00:03:47,070 --> 00:03:53,100 So if anything tries to import that file rather than actually importing the real file just is going 53 00:03:53,100 --> 00:03:57,550 to redirect that import and give whatever file is trying to import Nat's wrapper. 54 00:03:57,630 --> 00:04:03,020 Our implementation we wrote out inside of underscore underscore mocks I'm not going to save this this 55 00:04:03,020 --> 00:04:07,850 you know our tests are not going to run correctly just yet because we do not have any fake implementation 56 00:04:07,880 --> 00:04:12,230 inside of Nat's wrapper but we should at least see some kind of different error messages inside of our 57 00:04:12,230 --> 00:04:17,690 tests that at least tell us that this file is being required in or imported instead of the real Nats 58 00:04:17,690 --> 00:04:18,690 wrapper. 59 00:04:18,710 --> 00:04:20,990 Let's try this out. 60 00:04:21,070 --> 00:04:22,810 I'm gonna flip back over. 61 00:04:22,810 --> 00:04:24,700 I'm gonna restart my test suite entirely 62 00:04:28,610 --> 00:04:30,500 and we're probably going to still see a lot of errors. 63 00:04:30,520 --> 00:04:37,230 So I'm going to try to import only urged me run tests only for the new test file that you only run the 64 00:04:37,260 --> 00:04:39,330 test inside of that new file. 65 00:04:39,330 --> 00:04:44,610 I'm going to type into this terminal window the letter P which is going to allow us to only run tests 66 00:04:44,610 --> 00:04:48,110 with a very specific file name of press P. 67 00:04:48,110 --> 00:04:51,010 I then get prompted on which file I want to try to run. 68 00:04:51,240 --> 00:04:58,770 I'm going to put in new that's gonna try to find the new test t s file though I'll now see that it's 69 00:04:58,770 --> 00:05:04,500 only executing at that one test file alone and now we're going to get a very different error message 70 00:05:04,530 --> 00:05:06,110 compared to what we got before. 71 00:05:06,150 --> 00:05:10,740 Previously it said oh you have to initialize the client or connect with it before you make use of it. 72 00:05:10,740 --> 00:05:15,300 Now we're seeing a very different error message one that says cannot read property published of undefined 73 00:05:15,630 --> 00:05:21,660 which makes me think at least tentatively that this fake implementation is being used instead of our 74 00:05:21,660 --> 00:05:23,290 real one. 75 00:05:23,420 --> 00:05:26,800 OK so that's how we kind of set up this import redirecting stuff. 76 00:05:27,170 --> 00:05:29,760 So now that this is working we're going to take a quick pause. 77 00:05:29,870 --> 00:05:34,400 Come back next video and figure out how we can somehow write out a useful fake implementation.