]>
Commit | Line | Data |
---|---|---|
af37210c JM |
1 | import * as child from 'child_process' |
2 | ||
3 | class MockSmtpServer { | |
4 | ||
5 | private static instance: MockSmtpServer | |
6 | private started = false | |
7 | private emailChildProcess: child.ChildProcess | |
8 | private emails: object[] | |
9 | ||
10 | private constructor () { | |
11 | this.emailChildProcess = child.fork(`${__dirname}/email-child-process`, [], { silent: true }) | |
12 | this.emailChildProcess.on('message', (msg) => { | |
13 | if (msg.email) { | |
14 | return this.emails.push(msg.email) | |
15 | } | |
16 | }) | |
17 | process.on('exit', () => { | |
18 | this.emailChildProcess.kill() | |
f076daa7 | 19 | }) |
af37210c JM |
20 | } |
21 | ||
22 | collectEmails (emailsCollection: object[]) { | |
23 | return new Promise((res, rej) => { | |
24 | if (this.started) { | |
25 | this.emails = emailsCollection | |
26 | return res() | |
27 | } | |
28 | ||
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')) | |
34 | }) | |
35 | this.emailChildProcess.on('message', (msg) => { | |
36 | if (msg.err) { | |
37 | return rej(new Error(msg.err)) | |
38 | } | |
39 | this.started = true | |
40 | this.emails = emailsCollection | |
41 | return res() | |
42 | }) | |
43 | }) | |
44 | } | |
45 | ||
46 | static get Instance () { | |
47 | return this.instance || (this.instance = new this()) | |
48 | } | |
f076daa7 C |
49 | } |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | export { | |
af37210c | 54 | MockSmtpServer |
f076daa7 | 55 | } |