# Testing event listeners in JavaScript

DevFeed: [Testing event listeners in JavaScript](<https://devfeed.tech/articles/testing-event-listeners-in-javascript-52564.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/testing-event-listeners-javascript/>)

Published: 2020-07-14T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [Unit testing](<https://devfeed.tech/topics/unit-testing.md>), [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [Jest](<https://devfeed.tech/topics/jest.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Publish-subscribe pattern](<https://devfeed.tech/topics/pubsub.md>), [Test-driven development](<https://devfeed.tech/topics/tdd.md>)

Tags: [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [event](<https://devfeed.tech/tags/event.md>), [event-listeners](<https://devfeed.tech/tags/event-listeners.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

A test can pass without checking an event if its only assertion sits inside a listener callback that never runs. The excerpt recommends deliberately making each new test fail once, then offers two Jest fixes: `expect.assertions(1)` catches a missing assertion, while a `jest.fn()` spy lets the test assert that the handler ran once and received the expected data. The spy approach produces a more specific failure message. The final example follows arrange, act, assert: set up the page and spy, register the handler and make the request, then assert call count and arguments. SinonJS is suggested for spies when not using Jest.

## Source excerpt

Testing JavaScript code that's asynchronous can prevent its own set of challenges that you have to ensure you deal with when writing your tests. I recently came across a test that fell foul to the asynchronous code it was trying to test and in this post I'll show you how to look out for these tests and how to fix it. The problem Let's imagine we're working on an API that lets you make requests to load pages in a web browser. We have a Page class that provides the makeRequest method that will emit a request_started event: const EventEmitter = require('events') class Page extends EventEmitter { makeRequest(url) { this.emit('request_started', { url }) } } This means any other part of this codebase can listen out for these events: page.on('request_started', () => { /* do something here */ }) This is useful functionality, so let's write a test for it: describe('Page class', () => { it('emits an event when a request is started', () => { const page = new Page() page.on('request_started', (data) => { expect(data.url).toEqual('www.foo.com') }) page.makeRequest('www.foo.com') }) }) This test passes, but it's hiding a problem. What happens if we deliberately break it? If we remove the this.emit line, look what the test outputs: PASS src/site/code-for-posts/async-tests/async-tests.test.js Page class ✓ emits an event when a request is started (6 ms) This is less good 😒. But why does it pass? If we take a look at the test body, think about what happens when the request_started event never fires. Which of the lines below will end up being executed? it('emits an event when a request is started', () => { const page = new Page() page.on('request_started', (data) => { expect(data.url).toEqual('www.foo.com') }) page.makeRequest('www.foo.com') }) Because our expect call is within the event listener callback, it never runs if the event never fires! This is problematic because most test frameworks assume a test that doesn't explicitly fail is passing. Most test frameworks won't notify you