aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/miscs/email.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/miscs/email.ts')
-rw-r--r--shared/extra-utils/miscs/email.ts39
1 files changed, 17 insertions, 22 deletions
diff --git a/shared/extra-utils/miscs/email.ts b/shared/extra-utils/miscs/email.ts
index 758b15b58..9fc9a5ad0 100644
--- a/shared/extra-utils/miscs/email.ts
+++ b/shared/extra-utils/miscs/email.ts
@@ -1,8 +1,9 @@
1import { ChildProcess, fork } from 'child_process' 1import { ChildProcess } from 'child_process'
2import { join } from 'path'
3import { randomInt } from '../../core-utils/miscs/miscs' 2import { randomInt } from '../../core-utils/miscs/miscs'
4import { parallelTests } from '../server/servers' 3import { parallelTests } from '../server/servers'
5 4
5const MailDev = require('maildev')
6
6class MockSmtpServer { 7class MockSmtpServer {
7 8
8 private static instance: MockSmtpServer 9 private static instance: MockSmtpServer
@@ -10,38 +11,32 @@ class MockSmtpServer {
10 private emailChildProcess: ChildProcess 11 private emailChildProcess: ChildProcess
11 private emails: object[] 12 private emails: object[]
12 13
13 private constructor () { 14 private constructor () { }
14 this.emailChildProcess = fork(join(__dirname, 'email-child-process'), [])
15
16 this.emailChildProcess.on('message', (msg: any) => {
17 if (msg.email) {
18 return this.emails.push(msg.email)
19 }
20 })
21
22 process.on('exit', () => this.kill())
23 }
24 15
25 collectEmails (emailsCollection: object[]) { 16 collectEmails (emailsCollection: object[]) {
26 return new Promise<number>((res, rej) => { 17 return new Promise<number>((res, rej) => {
27 const port = parallelTests() ? randomInt(1000, 2000) : 1025 18 const port = parallelTests() ? randomInt(1000, 2000) : 1025
19 this.emails = emailsCollection
28 20
29 if (this.started) { 21 if (this.started) {
30 this.emails = emailsCollection
31 return res(undefined) 22 return res(undefined)
32 } 23 }
33 24
34 // ensure maildev isn't started until 25 const maildev = new MailDev({
35 // unexpected exit can be reported to test runner 26 ip: '127.0.0.1',
36 this.emailChildProcess.send({ start: true, port }) 27 smtp: port,
37 this.emailChildProcess.on('exit', () => { 28 disableWeb: true,
38 return rej(new Error('maildev exited unexpectedly, confirm port not in use')) 29 silent: true
30 })
31
32 maildev.on('new', email => {
33 this.emails.push(email)
39 }) 34 })
40 this.emailChildProcess.on('message', (msg: any) => { 35
41 if (msg.err) return rej(new Error(msg.err)) 36 maildev.listen(err => {
37 if (err) return rej(err)
42 38
43 this.started = true 39 this.started = true
44 this.emails = emailsCollection
45 40
46 return res(port) 41 return res(port)
47 }) 42 })