Testing
Facteur provides a fake implementation to capture notifications during tests instead of sending them. This allows you to verify that notifications are being sent correctly without actually delivering them.
Fake notifications
You can use the fake method to swap the notification sender with a fake implementation. All notifications sent after calling fake will be captured and stored in memory.
import { test } from '@japa/runner'
import { facteur } from '#services/facteur'
import { WelcomeNotification } from '#notifications/welcome'
test('sends welcome notification to new users', async () => {
const fake = facteur.fake()
await facteur
.notification(WelcomeNotification)
.to(user)
.send()
fake.assertSentCount(1)
facteur.restore()
})
Once done testing, you must call the restore method to restore the original sender.
Assertions
The fake instance provides the following assertion methods.
assertSentCount
Assert the total number of notifications sent.
fake.assertSentCount(2)
You can also pass a notification class to assert the count for a specific notification type.
fake.assertSentCount(WelcomeNotification, 1)
assertNoneSent
Assert that no notifications were sent.
fake.assertNoneSent()
assertSent
Assert that a specific notification was sent. You can optionally pass a callback to perform additional assertions on the captured notification.
import assert from 'node:assert'
fake.assertSent(WelcomeNotification, (sent) => {
assert.equal(sent.to.email, 'user@example.com')
})
Accessing captured notifications
You can access the captured notifications using the sent method.
const all = fake.sent()
const welcomeOnly = fake.sent(WelcomeNotification)
Each captured notification contains:
notification- The notification instanceto- The recipient (notifiable)params- The parameters passed to the notificationvia- The channels used to send the notification
Clearing captured notifications
If you need to reset the captured notifications within the same test, use the clear method.
fake.clear()
Note that calling facteur.fake() creates a fresh instance, so you don't need to call clear() between tests if you call fake() at the beginning of each test.