adonisjs / mail

AdonisJS Email Provider
MIT License
106 stars 34 forks source link

Error when testing mail #95

Closed unkobweb closed 8 months ago

unkobweb commented 8 months ago

Package version

9.2.0

Describe the bug

Hi, i tried to do like the documentation :

import { test } from '@japa/runner'
import mail from '@adonisjs/mail/services/main'
import VerifyEmailNotification from '#mails/verify_email'

test.group('Users | register', () => {
  test('create a new user account', async ({ client, route }) => {
    /**
     * Turn on the fake mode
     */
    const { mails } = mail.fake()

    /**
     * Make an API call
     */
    await client
      .post(route('users.store'))
      .send(userData)

    /**
     * Assert the controller indeed sent the
     * VerifyEmailNotification mail
     */
    mails.assertSent(VerifyEmailNotification, ({ message }) => {
      return message
        .hasTo(userData.email)
        .hasSubject('Verify email address')
    })
  })
})

And here is my code with all errors : Capture d'écran 2024-03-15 000728 And here is my mail class :

import env from '#start/env'
import { BaseMail } from '@adonisjs/mail'

export default class SendPasswordTokenNotification extends BaseMail {
  from = env.get('SENDER_EMAIL')
  subject = 'Votre code de réinitialisation de mot de passe'

  to: string
  payload: any

  constructor(private email: string, private data: { token: string }) {
    super()
    this.to = email
    this.payload = data
  }
  /**
   * The "prepare" method is called automatically when
   * the email is sent or queued.
   */
  prepare() {
    this.message.to(this.to)
    this.message.htmlView('emails/send_password_token', this.payload)
  }
}

(I don't know if it's the good way to pass data in the mail class but the documentation doesn't show it :/)

Reproduction repo

No response

thetutlage commented 8 months ago

There was an issue with assertSent method and other similar methods where they did not allow Mail classes that accepts constructor argument. This issue has been fixed.

Another issue is with the docs related to the following code snippet.

return message
        .hasTo(userData.email)
        .hasSubject('Verify email address')

It should be

return message.hasTo(userData.email) && message.hasSubject('Verify email address')

Will fix the docs next