Skip to content

Commit 3fb51ab

Browse files
committed
feat: add support for Symbol.dispose
1 parent 6fa41d2 commit 3fb51ab

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/fake_mailer.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,16 @@ class MessagesCollection {
527527
export class FakeMailer extends Mailer<JSONTransport> implements MailerContract<JSONTransport> {
528528
mails = new MailsCollection()
529529
messages = new MessagesCollection()
530+
#onDispose?: () => void
530531

531-
constructor(name: string, emitter: EmitterLike<MailEvents>, config: MailerConfig) {
532+
constructor(
533+
name: string,
534+
emitter: EmitterLike<MailEvents>,
535+
config: MailerConfig,
536+
onDispose?: () => void
537+
) {
532538
super(name, new JSONTransport(), emitter, config)
539+
this.#onDispose = onDispose
533540
super.setMessenger({
534541
queue: async (mail, sendConfig) => {
535542
return this.sendCompiled(mail, sendConfig)
@@ -585,6 +592,10 @@ export class FakeMailer extends Mailer<JSONTransport> implements MailerContract<
585592
}, config)
586593
}
587594

595+
[Symbol.dispose]() {
596+
this.#onDispose?.()
597+
}
598+
588599
async close() {
589600
this.messages.clear()
590601
this.mails.clear()

src/mail_manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class MailManager<KnownMailers extends Record<string, MailManagerTranspor
148148
this.restore()
149149

150150
debug('creating fake mailer')
151-
this.#fakeMailer = new FakeMailer('fake', this.#emitter, this.config)
151+
this.#fakeMailer = new FakeMailer('fake', this.#emitter, this.config, () => this.restore())
152152
return this.#fakeMailer
153153
}
154154

tests/integration/mail_manager.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,36 @@ test.group('Mail manager', () => {
296296
assert.lengthOf(messages.sent(), 0)
297297
})
298298

299+
test('restore fake mailer using Symbol.dispose', async ({ assert }) => {
300+
const emitter = new Emitter<MailEvents>(app)
301+
302+
const mail = new MailManager(emitter, {
303+
mailers: {
304+
mailer1: () => new JSONTransport(),
305+
},
306+
})
307+
308+
let messages: ReturnType<typeof mail.fake>['messages']
309+
310+
{
311+
using fake = mail.fake()
312+
await mail.use('mailer1').send((message) => {
313+
message.to('foo@bar.com')
314+
message.subject('Verify email address')
315+
})
316+
317+
fake.messages.assertSent({ subject: 'Verify email address' })
318+
assert.lengthOf(fake.messages.sent(), 1)
319+
messages = fake.messages
320+
}
321+
322+
/**
323+
* After the block, the fake mailer should be disposed
324+
* and messages should be cleared
325+
*/
326+
assert.lengthOf(messages!.sent(), 0)
327+
})
328+
299329
test('close all mailers and remove from cache', async ({ assert }) => {
300330
const emitter = new Emitter<MailEvents>(app)
301331
const smtpTransport = new SMTPTransport({

0 commit comments

Comments
 (0)