Loading
Current section: Functions 5 exercises
solution

Mock functions

Loading solution

Transcript

00:00 So in our test suite for the emitter class, we have this single test that constructs the emitter. This emitter understands the hello event that has data, specifically first name, which is a string. So the first thing I will do to test this emitter is create a mock function. I will call it a listener and it will equal v.fn.

00:18 v is a special object exported from vTest, but you may notice that we are not importing it anywhere. That's because I'm using it as a global object. I do this by two things. The first one is in my vTest config, I enable test.globals to be true. And the second one is in my TypeScript configuration. I'm adding vTest slash globals to my types.

00:38 This will make TypeScript and Node.js understand what are the v object, or the test function, or the expect function. I advise you to set up them as global functions, because you'll be using them pretty much in every test. So here, our mock listener function looks like an empty function. It doesn't do anything. But in fact, that's not true.

00:57 What vTest does with this v.fn call is imbues these functions with special powers. One of those powers is that we now are able to observe all the calls to this listener. So for example, if I'm logging out listener.mock.calls, I will see that right now nothing called this listener function. So for example, if I call it with some data, like hello world,

01:19 here I see this call recorded. I will use this special powers to assert that my emitter class indeed calls this listener function right amount of times and with right arguments. So let's proceed. We have this listener, and now we need to attach the event. So I will use emitter on hello, which is a known event, and supplying listener as an argument.

01:41 The next step, I will emit this hello event, so my emitter class will indeed call this listener. I will call emitter.emit, provide the hello event, and provide some data as a username, for example, John. Now, this is the action phase of our setup, and it's done. Next is assertion.

01:59 I will expect that my listener function, my mock function, has been called once. You see, I don't have to access this internal listener.mock.calls array. Instead, I can use the built-in assertion from Vitas like this to have been called once. And the next assertion, I will make sure that the listener is called with the correct data.

02:19 So I will use it to have been called with assertion, and provide the data I expect, which is a John string. So with this in place, let's run our tests, and we can see them passing. So what's happening now here is that we're constructing this mock function using v.fn, provided as an input, as a listener to the hello event,

02:39 and when this event gets emitted with particular data, we're checking that this mock listener indeed has been called, and just once, because there has been one event emitted, and it has been called with the right data, which is John.