1 import { fork, ChildProcess } from 'child_process'
5 private static instance: MockSmtpServer
6 private started = false
7 private emailChildProcess: ChildProcess
8 private emails: object[]
10 private constructor () {
11 this.emailChildProcess = fork(`${__dirname}/email-child-process`, [])
13 this.emailChildProcess.on('message', (msg) => {
15 return this.emails.push(msg.email)
19 process.on('exit', () => this.kill())
22 collectEmails (emailsCollection: object[]) {
23 return new Promise((res, rej) => {
25 this.emails = emailsCollection
29 // ensure maildev isn't started until
30 // unexpected exit can be reported to test runner
31 this.emailChildProcess.send({ start: true })
32 this.emailChildProcess.on('exit', () => {
33 return rej(new Error('maildev exited unexpectedly, confirm port not in use'))
35 this.emailChildProcess.on('message', (msg) => {
37 return rej(new Error(msg.err))
40 this.emails = emailsCollection
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 // ---------------------------------------------------------------------------