Ready to get started?

Join Adocasts Plus for $8/mo, or sign into an existing Adocasts Plus account, to get access to all of our lessons.

robot mascot smiling

Pragmatic Testing in AdonisJS with Japa #3.3

Unit Testing Event Listeners

In This Lesson

Learn how to unit test AdonisJS Event Listeners with Japa. Use the Mail Fake to isolate the test, instantiate the listener, manually call the listener and pass data, and use assertions to confirm the expected email was queued.

Created by
@tomgobich
Published

Notes Used to Craft this Lesson

If you'll recall, our welcome email is being sent via an event listener.

// app/services/user_service.ts
export default class UserService {
  /**
   * Sends a welcome email to the user.
   * The email is sent asynchronously and will not block the execution of the current code.
   * @param data - An object containing the user's full name and email address
   * @returns A promise resolving when the email is sent
   */
  async sendWelcomeEmail(data: { fullName: string | null; email: string }) {
++    await emitter.emit('mail:send_welcome_email', data)
  }
}

Join the Discussion 0 comments

Create a free account to join in on the discussion
robot comment bubble

Be the first to comment!