> For the complete documentation index, see [llms.txt](https://book.restfulnode.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.restfulnode.com/part-3/chapter-10/5-testing-the-event.md).

# Testing the Event

Recall how we implemented the `userHasRegisteredEvent`.

*File: src/events/userHasRegisteredEvent.js*

```javascript
const mailer = require('../utils/mailer')

/**
 *
 * @param {*} user
 *
 * @returns boolean
 */
const userHasRegisteredEvent = (user) => {
  const bodyText = `Hello ${user.firstName}. Thanks for registering!`
  return mailer.sendEmailToUser(user.email, 'Welcome aboard!', bodyText)
}

module.exports = userHasRegisteredEvent
```

Now for this test, we actually don't care that much about the `mailer` utility and whether or not we actually sent an email. What we are really interested in is whether or not the `mailer.sendEmailToUser` was called or not. For this particular test, it would be sufficient to just mock up the `mailer` and the `sendEmailToUser` method to check if they've been called.

### The Test

The mocking part for this test is a lot tricky, but notice how we mock both the `mailer` and the `sendEmailToToUser` below.

*File: src/events/\_\_tests\_\_/userHasRegisteredEvent.test.js*

```javascript
const event = require('../userHasRegisteredEvent')
const mailer = require('../../utils/mailer')

const sentEmailSuccessful = !!Math.round(Math.random(0, 1))
jest.mock('../../utils/mailer')
mailer.sendEmailToUser.mockImplementation(() => sentEmailSuccessful)

describe('Test Suite: userHasRegisteredEvent', () => {
  test("Email has (un)successfully been sent to the user's email address", () => {
    const eventResult = event({
      firstName: 'John',
      email: 'john@john.com'
    })

    mailer.sendEmailToUser.mockImplementation(() => true)

    expect(mailer.sendEmailToUser).toHaveBeenCalledTimes(1)
    expect(eventResult).toBe(sentEmailSuccessful)
  })
})
```

This might look a little strange at first, but one aspect to consider is that you should not be sending an actual email when running the test. Imagine a scenario in which we did not mock the `mailer` utility and its `sendEmailToUser` method, and we actually stubbed in real inputs into the function, then we would be making real life calls that would send real emails.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.restfulnode.com/part-3/chapter-10/5-testing-the-event.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
