1 00:00:00,870 --> 00:00:04,710 The next thing we're going to fix instead of our ticketing service is our test suite. 2 00:00:04,710 --> 00:00:06,740 We spent a pretty good amount of time writing out some tests. 3 00:00:06,750 --> 00:00:08,660 Out of all of our different route handlers. 4 00:00:08,880 --> 00:00:13,530 But if we run our test suite right now we're gonna see that just about all of them are failing. 5 00:00:13,630 --> 00:00:19,320 So I got to go back or my terminal inside of my tickets service I'll do an NPM run test to run all my 6 00:00:19,320 --> 00:00:26,520 tests and then after a very brief warm up period we will see there we go. 7 00:00:26,590 --> 00:00:33,070 A ton of failures though it looks like the vast majority of these failures are being caused by not access 8 00:00:33,090 --> 00:00:35,230 Nats client people are connecting. 9 00:00:35,230 --> 00:00:39,550 There are some others inside of here around Mongoose air but let me just tell you right now. 10 00:00:39,550 --> 00:00:45,540 These are essentially being thrown because of the failed Nats client stuff so this all started to fail. 11 00:00:45,540 --> 00:00:49,290 Once we added in the event publishing why is it failing. 12 00:00:49,290 --> 00:00:51,230 Well it's pretty straightforward. 13 00:00:51,270 --> 00:00:56,760 So whenever we are running our application like inside of our development cabinet is cluster we've got 14 00:00:56,820 --> 00:01:03,160 our new ticket root handler or the update ticket root handler and inside of those we've got a publisher. 15 00:01:03,160 --> 00:01:06,700 The publishers need a Nats client whenever they get created. 16 00:01:06,730 --> 00:01:11,280 So we have some way of actually publishing an event when running our application. 17 00:01:11,280 --> 00:01:17,940 Normally we have that other Nats wrapper file inside there is the Nats wrapper Singleton and inside 18 00:01:17,940 --> 00:01:21,160 of that we have our pre initialized Nats clients. 19 00:01:21,360 --> 00:01:26,430 So we take that initialized Nats client we throw it into the publisher and everything works as expected 20 00:01:27,320 --> 00:01:28,640 but in our test environment. 21 00:01:28,890 --> 00:01:31,990 Well not quite so nice in our test environment. 22 00:01:32,010 --> 00:01:37,050 At no point time do we actually initialize the Nats client so it is not initialized. 23 00:01:37,050 --> 00:01:43,460 We have not create the Nats client inside of that Nats wrapper Singleton however we still import that 24 00:01:43,460 --> 00:01:46,340 thing over into the ticket created publisher. 25 00:01:46,340 --> 00:01:51,470 We provide it and eventually when our publisher tries to access that client we throw that nasty error 26 00:01:51,470 --> 00:01:52,640 message that says 27 00:01:55,220 --> 00:02:00,260 right here cannot access Nats client before connecting. 28 00:02:00,300 --> 00:02:02,280 So how are we gonna fix this. 29 00:02:02,280 --> 00:02:04,290 There's definitely two approaches we can take. 30 00:02:04,290 --> 00:02:10,800 We can either try to have our test environment actually connect to our NAT server that's not super ideal 31 00:02:10,890 --> 00:02:15,450 because we don't really want to assume that we always have some running at Nats event bus whenever we're 32 00:02:15,510 --> 00:02:20,970 trying to run our tests that require us to always be running some copy of Nats on our local machine 33 00:02:21,270 --> 00:02:23,540 or some other mechanism to run it. 34 00:02:23,900 --> 00:02:25,400 So not super ideal. 35 00:02:25,470 --> 00:02:28,930 Maybe there's another way to handle this and naturally there is. 36 00:02:29,090 --> 00:02:30,310 Here's what we're going to do. 37 00:02:31,410 --> 00:02:34,830 We're going to use a fancy little feature inside of jets. 38 00:02:34,860 --> 00:02:39,420 You might recall that just as our test runner just as what we are actually running when we execute our 39 00:02:39,420 --> 00:02:44,070 tests inside the terminal when we execute our tests we are using the just test runner. 40 00:02:44,250 --> 00:02:50,030 And there's also the just library running inside of our test suite as well just as a fancy little feature. 41 00:02:50,100 --> 00:02:56,330 We essentially redirect import statements while we are running our application in the test environment. 42 00:02:56,450 --> 00:03:02,160 So at some point in time our new ticket route handler attempts to import the Nats wrapper file. 43 00:03:02,370 --> 00:03:05,460 That's kind of represented by this little white box right here. 44 00:03:05,460 --> 00:03:08,990 We're going to get just to intercept that import statements. 45 00:03:09,090 --> 00:03:13,470 It's going to see that this file is trying to import the real Nats wrapper and we're going to instead 46 00:03:13,470 --> 00:03:20,660 have just add a redirect that import statement and import a fake copy of the Nats wrapper instead and 47 00:03:20,720 --> 00:03:24,430 inside there we will have a fake initialized Nats client. 48 00:03:24,530 --> 00:03:26,310 I say fake initialize Nats client. 49 00:03:26,390 --> 00:03:27,560 I really do mean fake. 50 00:03:27,560 --> 00:03:31,310 We're not going to actually connect to the NAT server or anything like that. 51 00:03:31,370 --> 00:03:36,680 We're going to instead kind of trick everything into thinking that this is a real Nats client but in 52 00:03:36,680 --> 00:03:39,070 fact not real at all. 53 00:03:39,250 --> 00:03:43,690 That's a general idea at this point but it seems a bit weird I'm sure. 54 00:03:43,690 --> 00:03:48,550 But it turns out the code we have to write out to actually implement this stuff is pretty straightforward. 55 00:03:48,550 --> 00:03:51,790 Let's take a pause right here and figure out how to put this stuff together in the next video.