1 import { ChildProcess } from 'child_process'
2 import MailDev from 'maildev'
3 import { randomInt } from '@shared/core-utils'
4 import { parallelTests } from '../miscs'
8 private static instance: MockSmtpServer
9 private started = false
10 private emailChildProcess: ChildProcess
11 private emails: object[]
13 private constructor () { }
15 collectEmails (emailsCollection: object[]) {
16 return new Promise<number>((res, rej) => {
17 const port = parallelTests() ? randomInt(1000, 2000) : 1025
18 this.emails = emailsCollection
24 const maildev = new MailDev({
31 maildev.on('new', email => {
32 this.emails.push(email)
35 maildev.listen(err => {
36 if (err) return rej(err)
46 if (!this.emailChildProcess) return
48 process.kill(this.emailChildProcess.pid)
50 this.emailChildProcess = null
51 MockSmtpServer.instance = null
54 static get Instance () {
55 return this.instance || (this.instance = new this())
59 // ---------------------------------------------------------------------------