Transcript
00:00 In this exercise, you will be testing a class called Emitter. It looks a little bit like Event Emitter from Node.js if you used it before. So let's take a look at how it works. This class has two methods. One of them is On, which accepts the event and a listener function associated with that event, and just stores it internally in this listeners map.
00:18 And then there's Emit method that accepts the event name and also some data associated with this event. And then it looks up if there are any matching listeners, and if there are, it calls them one by one supplying this particular data. One common mistake when testing this functionality is this. In this test example, we're constructing our emitter,
00:36 and then we're introducing this internal state called IsListenerCalled that is false by default. So then we can attach a listener to the hello event that's going to update that state to true. Then we emit this event, and of course assert that IsListenerCalled now equals true. And on the surface, yeah, this test will pass, but it's a bad test.
00:57 Let me show you why. If you forget to update this internal state, well, your test will fail because nothing updated this variable to true. But you know the funny part? The listener is still called. You can see this through this console message still being printed. So this is a huge red flag, and it indicates that this test violates the golden rule of assertion
01:19 that says that the test must only fail if the tested intention is not met. But here, the listener is getting called, but the test is still failing. The reason for that is that this particular test never tested the intention to begin with. It only tested our testing setup, this IsListenerCalled state. And since right now it's broken,
01:39 the test is failing, letting you know. In this exercise, you will learn how to test this functionality reliably using mock functions in btest. So head to emitter.test.ts file, and follow the instructions to complete this test suite. Once you're done, run the test via npm test in the terminal to see them passing.