This video we're going to take a look at how to get just to essentially fake out this import statement
or redirect it.
Here's the entire process.
This is what we're gonna do in this video.
The first we're going to do is find the file that we want to make in our case the file that we want
to fake is our nets wrapper dot t s file inside of our roots s our C directory.
So this and that's wrapped right here.
This has all the code that is going to try to actually connect to our real NAT server and we did not
want to run this code because we don't want to have to run a copy of nets when we are executing our
tests.
So this is some code that we want to somehow fake out so to do so we're going to find that file.
Well just found it then inside the same directory we're gonna create a folder called underscore underscore
box underscore underscore.
Let's do that right now.
Go inside the same directory which in this case to be s RC I'll make a new folder called underscore
underscore make sure you get two in their box underscore underscore
after that we're going to open up that folder we've just created and we're gonna create a file inside
there with identical names the file that we are trying to fake.
So in our case we are trying to fake or kind of duplicate or redirect Nat's wrapper.
Yes.
So you'll make an identically named file inside of underscore underscore Max.
So that's wrapper dot.
Yes then inside this file we're going to write out some fake implementation so in our case this is where
we're going to want to somehow fake out or provide some code to run instead of this Nats wrapper stuff
so to do so inside of the new file we just created we can do an export onsite Nat's wrapper and I'm
going to directly return an object.
So why do I need to do a export continental wrapper.
Well we are essentially trying to fake out or duplicate the functionality of the real Nats wrapper file.
The real Nats wrapper file if we go down to the bottom it exports a single named export of Nats wrapper
and that is going to be an instance of a class or essentially an object.
So we can't build up or export Nats wrapper and then this is the object that we are going to export
so we can write out our fake implementation or essentially some code to run instead of the real stuff
inside of this object.
Right now I'm going to leave this empty we'll come back and write in some fake implementation in just
a moment.
All right.
Now the last step we're going to make sure that we tell just to use that fake file we just put together
inside of our test file.
Right now we have a couple of different files that are essentially trying to make use of this Nats wrapper
stuff.
They're specifically the new and update set of tests so inside of my test directory we're going to first
focus on getting the new test.
Yes file working correctly so inside of their I'm going to add in an import statement at the top of
this new test file that's gonna tell just to make use of our mock file but to do so we'll write out
just not Mark and then we'll write out a relative path to that file that we want to fake.
So in this case the file we want to fake is Nat's wrapper inside the SRT RC directory that you write
out a relative path to it.
We would go up one directory from underscore underscored test into routes up another directory into
us our C and then we can get Nat's dash wrapper.
So in total up one directory of one directory that's dash wrapper that whenever these tests are executed
Justin's going to see that we are trying to mock that file.
So if anything tries to import that file rather than actually importing the real file just is going
to redirect that import and give whatever file is trying to import Nat's wrapper.
Our implementation we wrote out inside of underscore underscore mocks I'm not going to save this this
you know our tests are not going to run correctly just yet because we do not have any fake implementation
inside of Nat's wrapper but we should at least see some kind of different error messages inside of our
tests that at least tell us that this file is being required in or imported instead of the real Nats
wrapper.
Let's try this out.
I'm gonna flip back over.
I'm gonna restart my test suite entirely
and we're probably going to still see a lot of errors.
So I'm going to try to import only urged me run tests only for the new test file that you only run the
test inside of that new file.
I'm going to type into this terminal window the letter P which is going to allow us to only run tests
with a very specific file name of press P.
I then get prompted on which file I want to try to run.
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
only executing at that one test file alone and now we're going to get a very different error message
compared to what we got before.
Previously it said oh you have to initialize the client or connect with it before you make use of it.
Now we're seeing a very different error message one that says cannot read property published of undefined
which makes me think at least tentatively that this fake implementation is being used instead of our
real one.
OK so that's how we kind of set up this import redirecting stuff.
So now that this is working we're going to take a quick pause.
Come back next video and figure out how we can somehow write out a useful fake implementation.