1 import { ChildProcess } from 'child_process'
2 import MailDev from '@peertube/maildev'
3 import { parallelTests, randomInt } from '@shared/core-utils'
7 private static instance: MockSmtpServer
8 private started = false
9 private emailChildProcess: ChildProcess
10 private emails: object[]
12 private constructor () { }
14 collectEmails (emailsCollection: object[]) {
15 return new Promise<number>((res, rej) => {
16 const port = parallelTests() ? randomInt(1000, 2000) : 1025
17 this.emails = emailsCollection
23 const maildev = new MailDev({
30 maildev.on('new', email => {
31 this.emails.push(email)
34 maildev.listen(err => {
35 if (err) return rej(err)
45 if (!this.emailChildProcess) return
47 process.kill(this.emailChildProcess.pid)
49 this.emailChildProcess = null
50 MockSmtpServer.instance = null
53 static get Instance () {
54 return this.instance || (this.instance = new this())
58 // ---------------------------------------------------------------------------