1 import { ChildProcess } from 'child_process'
2 import { randomInt } from '../../core-utils/miscs/miscs'
3 import { parallelTests } from '../server/servers'
5 const MailDev = require('maildev')
9 private static instance: MockSmtpServer
10 private started = false
11 private emailChildProcess: ChildProcess
12 private emails: object[]
14 private constructor () { }
16 collectEmails (emailsCollection: object[]) {
17 return new Promise<number>((res, rej) => {
18 const port = parallelTests() ? randomInt(1000, 2000) : 1025
19 this.emails = emailsCollection
25 const maildev = new MailDev({
32 maildev.on('new', email => {
33 this.emails.push(email)
36 maildev.listen(err => {
37 if (err) return rej(err)
47 if (!this.emailChildProcess) return
49 process.kill(this.emailChildProcess.pid)
51 this.emailChildProcess = null
52 MockSmtpServer.instance = null
55 static get Instance () {
56 return this.instance || (this.instance = new this())
60 // ---------------------------------------------------------------------------