- In a web application, you typically have lots
of functions and objects, which call each other.
For example, function one might call function two
to get back a result.
Object one might invoke methods
on object two to do some work.
How do we deal with that when it comes to unit testing?
And because with unit testing, we're trying to focus
on the behavior of a single function or a single object,
we want to mask off any of its dependencies.
How can we test one function or an object
if it depends on others which have their own intricacies?
And the answer is mocking.
Jest has its own mock mechanism.
It has the concept of a mock function.
So an artificial replacement for another function
that you don't really want to call.
So in your unit tests,
what you can do is you can use Jest dot FN
to create a mock function.
You can then call that mock function like so,
so Jest will generate a mock function,
an artificial function,
which you can then invoke and it can return a result.
So let's see a simple example of how that works.
In my demo folder, I've got some simple code
in operations JS.
A function F one that doesn't really do anything much,
a function F two that takes some parameters,
so when you call that function, you pass in parameters,
a function F three, that returns a value.
Okay? So that's kinda like a full set
of different functions there,
no parameters, no return value.
Parameters, no return value,
return value, no parameters.
So that code is in operations JS.
If you look in the code folder
you'll see here operations JS.
That's the code that I've just shown you.
What we'll do now is we'll see how to mock those functions
in our tests.
So I've got some test code here that shows how
to create mocks as a replacement for those functions.
Okay. So first of all then.
The first test I'm gonna show you is really just showing how
to create a mock function
and how to invoke it, just to get used to the idea.
So in this test, I create a mock function.
I've called it mock F one.
It doesn't matter what I call this variable here.
It's just basically a pointer to that mock function.
I'll call it mock F one.
When I call it, I haven't really told Jest anything
about this mock function.
So what Jest will do when you invoke it,
it'll return undefined.
You can check here.
When I assert the result,
I find that the result is actually undefined.
Oh, and by the way,
also you can check was this mock function invoked once.
Okay? So mock functions remember information about how
many times they were called, what parameters were passed in,
and such like.
So it'll remember,
this mock function will remember that it's been called once.
Okay? And if I pass the new parameters
into the mock function,
the mock function would know what parameters were passed in.
So the mock function remembers how you've interacted
with it, which you can then check afterwards to see
if it was correct.
By default, unless you specify otherwise,
when you call a mock function, it returns undefined.
How could it possibly know what to return otherwise?
It's just a mock.
So by default, unless you set it up otherwise,
a mock function will always return undefined
as you've seen here.
Right. Let's have a look at another example.
This is a little bit more interesting.
This shows how you can pass parameters into a mock function.
So I've created a mock function,
I've called it mock F two.
When I call the function now,
I've passed in some parameters,
maybe to simulate some behavior
and it'll give me back a result.
And then I can have some assertions.
I've got three assertions here,
but it's the second one I'll concentrate on.
So first of all, I can assert
that the mock function has been called once.
Yes, of course it has.
I can check that the mock function was called
with these parameters.
Was this mock function invoked with these parameters?
Yes, it was. And what's the return value?
Well, the return value will be undefined
because we haven't given it any more clue
about what to return.
If you want to tell a mock function
what to return to simulate a real function's return value.
And this is how you do it.
When you call Jest FN, you basically provide a Lambda.
This here simulates the return value.
It's as if you set up a mock function,
which, when it's invoked, will return 42.
So here's my mock function.
When I call the mock function,
it'll basically run that code and it'll return 42.
So make sure that that function was invoked once.
Yes. And make sure that the result is 42.
Okay. So when you call the mock function,
it'll execute that Lambda.
This is the return value that gets assigned into here,
which you can then check if you want to.
Good. Right. Well, I guess we should run those tests.
just open up a command window and run Jest,
and they all pass.
There we go.
Okay? So that was a simple example,
an introduction to the concept of mocking and Jest.
You call the Jest dot F and function.
It returns you back a mock object.
You can specify, you can pass parameters
into it when you call it,
you can specify what it should return,
a mock return value as well.
What we'll do in the rest
of this lesson is to dig deeper into mock functions
to see how to use them properly in the context
of interacting functions and interacting objects.