-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (57 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var nodemailer = require('nodemailer')
var sendOptions
var sendCallback
var shouldFail = false
var suppliedCallback
var triggered = false
var response = {
message: 'Caught by mockmailer',
messageId: 'noid'
}
var error = new Error('failed by user request')
nodemailer.createTransport = function () {
return {
sendMail: function (opts, cb) {
sendOptions = opts
sendCallback = cb
triggered = true
delete response.options
delete response.sendCallback
if (typeof cb === 'function') {
cb(shouldFail ? error : null, response)
}
if (typeof suppliedCallback === 'function') {
response.options = sendOptions
response.callback = sendCallback
suppliedCallback(shouldFail ? error : null, response)
triggered = false
}
}
}
}
/**
* Supply a callback to be invoked when mail is sent that resolves or fails your test
* @method mockmailer
* @async
* @param {function} registeredCallback The callback to invoke when `sendMail` is called
* @returns {object} undefined
*/
var mockmailer = module.exports = function (registeredCallback) {
if (typeof registeredCallback === 'function') {
suppliedCallback = registeredCallback
if (triggered) {
response.options = sendOptions
response.callback = sendCallback
suppliedCallback(shouldFail ? error : null, response)
triggered = false
}
}
}
/**
* Sets nodemailer to either fail or not when `sendMail` is called
* @method setFail
* @param {boolean} mode Should nodemailer "fail" sending mail
*/
mockmailer.setFail = function (mode) {
shouldFail = !!mode
}