The next thing we're going to fix instead of our ticketing service is our test suite.
We spent a pretty good amount of time writing out some tests.
Out of all of our different route handlers.
But if we run our test suite right now we're gonna see that just about all of them are failing.
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
tests and then after a very brief warm up period we will see there we go.
A ton of failures though it looks like the vast majority of these failures are being caused by not access
Nats client people are connecting.
There are some others inside of here around Mongoose air but let me just tell you right now.
These are essentially being thrown because of the failed Nats client stuff so this all started to fail.
Once we added in the event publishing why is it failing.
Well it's pretty straightforward.
So whenever we are running our application like inside of our development cabinet is cluster we've got
our new ticket root handler or the update ticket root handler and inside of those we've got a publisher.
The publishers need a Nats client whenever they get created.
So we have some way of actually publishing an event when running our application.
Normally we have that other Nats wrapper file inside there is the Nats wrapper Singleton and inside
of that we have our pre initialized Nats clients.
So we take that initialized Nats client we throw it into the publisher and everything works as expected
but in our test environment.
Well not quite so nice in our test environment.
At no point time do we actually initialize the Nats client so it is not initialized.
We have not create the Nats client inside of that Nats wrapper Singleton however we still import that
thing over into the ticket created publisher.
We provide it and eventually when our publisher tries to access that client we throw that nasty error
message that says
right here cannot access Nats client before connecting.
So how are we gonna fix this.
There's definitely two approaches we can take.
We can either try to have our test environment actually connect to our NAT server that's not super ideal
because we don't really want to assume that we always have some running at Nats event bus whenever we're
trying to run our tests that require us to always be running some copy of Nats on our local machine
or some other mechanism to run it.
So not super ideal.
Maybe there's another way to handle this and naturally there is.
Here's what we're going to do.
We're going to use a fancy little feature inside of jets.
You might recall that just as our test runner just as what we are actually running when we execute our
tests inside the terminal when we execute our tests we are using the just test runner.
And there's also the just library running inside of our test suite as well just as a fancy little feature.
We essentially redirect import statements while we are running our application in the test environment.
So at some point in time our new ticket route handler attempts to import the Nats wrapper file.
That's kind of represented by this little white box right here.
We're going to get just to intercept that import statements.
It's going to see that this file is trying to import the real Nats wrapper and we're going to instead
have just add a redirect that import statement and import a fake copy of the Nats wrapper instead and
inside there we will have a fake initialized Nats client.
I say fake initialize Nats client.
I really do mean fake.
We're not going to actually connect to the NAT server or anything like that.
We're going to instead kind of trick everything into thinking that this is a real Nats client but in
fact not real at all.
That's a general idea at this point but it seems a bit weird I'm sure.
But it turns out the code we have to write out to actually implement this stuff is pretty straightforward.
Let's take a pause right here and figure out how to put this stuff together in the next video.