nodemailer-mock
Advanced tools
Comparing version 1.5.1 to 1.5.2
@@ -11,19 +11,21 @@ 'use strict'; | ||
let transport = null; | ||
let mockedVerify = true; | ||
let _mockedVerify = true; | ||
// our response messages | ||
let successResponse = messages.success_response; | ||
let failResponse = messages.fail_response; | ||
let _successResponse = messages.success_response; | ||
let _failResponse = messages.fail_response; | ||
// Sent mail cache | ||
let sentMail = []; | ||
const _userPlugins = { | ||
// transport plugins | ||
const _userPluginsDefault = { | ||
compile: [], | ||
stream: [], | ||
}; | ||
let _userPlugins = { ..._userPluginsDefault }; | ||
// Sent mail cache | ||
let _sentMail = []; | ||
// Should the callback be a success or failure? | ||
let shouldFail = false; | ||
let shouldFailOnce = false; | ||
let shouldFailCheck = null; | ||
let _shouldFail = false; | ||
let _shouldFailOnce = false; | ||
let _shouldFailCheck = null; | ||
@@ -34,10 +36,10 @@ // Determine if the test should return success or failure | ||
// if shouldFailCheck defined use it | ||
if (email && shouldFailCheck && shouldFailCheck(email)) { | ||
if (email && _shouldFailCheck && _shouldFailCheck(email)) { | ||
return reject(new Error('nodemailer-mock fail response')); | ||
} | ||
// determine if we want to return an error | ||
if (shouldFail) { | ||
if (_shouldFail) { | ||
// if this is a one time failure, reset the status | ||
if (shouldFailOnce) { | ||
shouldFail = shouldFailOnce = false; | ||
if (_shouldFailOnce) { | ||
_shouldFail = _shouldFailOnce = false; | ||
} | ||
@@ -70,5 +72,5 @@ return reject(new Error('nodemailer-mock fail response')); | ||
// add the email to our cache | ||
sentMail.push(email); | ||
_sentMail.push(email); | ||
// update the response | ||
info.response = successResponse; | ||
info.response = _successResponse; | ||
// indicate that we are sending success | ||
@@ -85,10 +87,10 @@ debug('transport.sendMail', 'SUCCESS', info); | ||
// update the response | ||
info.response = failResponse; | ||
info.response = _failResponse; | ||
// indicate that we are sending an error | ||
debug('transport.sendMail', 'FAIL', failResponse, info); | ||
debug('transport.sendMail', 'FAIL', _failResponse, info); | ||
// return the error | ||
if (isPromise) { | ||
return Promise.reject(failResponse); | ||
return Promise.reject(_failResponse); | ||
} | ||
return callback(failResponse, info); | ||
return callback(_failResponse, info); | ||
}); | ||
@@ -99,3 +101,3 @@ }, | ||
// should we mock the verify request? | ||
if (mockedVerify) { | ||
if (_mockedVerify) { | ||
// support either callback or promise api | ||
@@ -105,4 +107,4 @@ const isPromise = !callback && typeof Promise === 'function'; | ||
return determineResponseSuccess() | ||
.then(() => isPromise ? Promise.resolve(successResponse) : callback(null, successResponse)) | ||
.catch(() => isPromise ? Promise.reject(failResponse) : callback(failResponse)); | ||
.then(() => isPromise ? Promise.resolve(_successResponse) : callback(null, _successResponse)) | ||
.catch(() => isPromise ? Promise.reject(_failResponse) : callback(_failResponse)); | ||
} | ||
@@ -114,7 +116,7 @@ // use the real nodemailer transport to verify | ||
use: (step, plugin) => { | ||
step = (step || '').toString(); | ||
if (!Object.prototype.hasOwnProperty.call(_userPlugins, step)) { | ||
_userPlugins[step] = [plugin]; | ||
const stepId = (step || '').toString(); | ||
if (!Object.prototype.hasOwnProperty.call(_userPlugins, stepId)) { | ||
_userPlugins[stepId] = [plugin]; | ||
} else { | ||
_userPlugins[step].push(plugin); | ||
_userPlugins[stepId].push(plugin); | ||
} | ||
@@ -127,2 +129,8 @@ | ||
options, | ||
/** | ||
* get a dictionary of plugins used by this transport | ||
* @return {Object} plugins keyed by the step id | ||
*/ | ||
getPlugins: () => _userPlugins, | ||
}, | ||
@@ -138,3 +146,3 @@ }; | ||
setShouldFailOnce: () => { | ||
shouldFail = shouldFailOnce = true; | ||
_shouldFail = _shouldFailOnce = true; | ||
}, | ||
@@ -147,3 +155,3 @@ | ||
setShouldFail: (isFail) => { | ||
shouldFail = isFail; | ||
_shouldFail = isFail; | ||
}, | ||
@@ -156,3 +164,3 @@ | ||
setMockedVerify: (isMocked) => { | ||
mockedVerify = isMocked; | ||
_mockedVerify = isMocked; | ||
}, | ||
@@ -165,3 +173,3 @@ | ||
setSuccessResponse: (response) => { | ||
successResponse = response; | ||
_successResponse = response; | ||
}, | ||
@@ -174,3 +182,3 @@ | ||
setFailResponse: (error) => { | ||
failResponse = error; | ||
_failResponse = error; | ||
}, | ||
@@ -183,3 +191,3 @@ | ||
setShouldFailCheck: (check) => { | ||
shouldFailCheck = check; | ||
_shouldFailCheck = check; | ||
}, | ||
@@ -191,3 +199,3 @@ | ||
*/ | ||
getSentMail: () => sentMail, | ||
getSentMail: () => _sentMail, | ||
@@ -198,8 +206,9 @@ /** | ||
reset: () => { | ||
sentMail = []; | ||
shouldFail = shouldFailOnce = false; | ||
successResponse = messages.success_response; | ||
failResponse = messages.fail_response; | ||
mockedVerify = true; | ||
shouldFailCheck = null; | ||
_userPlugins = { ..._userPluginsDefault }; | ||
_sentMail = []; | ||
_shouldFail = _shouldFailOnce = false; | ||
_successResponse = messages.success_response; | ||
_failResponse = messages.fail_response; | ||
_mockedVerify = true; | ||
_shouldFailCheck = null; | ||
}, | ||
@@ -206,0 +215,0 @@ }; |
{ | ||
"name": "nodemailer-mock", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"description": "Mock nodemailer module for testing", | ||
@@ -44,7 +44,7 @@ "main": "nodemailer-mock.js", | ||
"depcheck": "0.9.2", | ||
"eslint": "6.8.0", | ||
"eslint-plugin-mocha": "6.3.0", | ||
"eslint": "7.3.1", | ||
"eslint-plugin-mocha": "7.0.1", | ||
"istanbul-cobertura-badger": "1.3.1", | ||
"mocha": "7.0.1", | ||
"nyc": "15.0.0", | ||
"mocha": "8.0.1", | ||
"nyc": "15.1.0", | ||
"should": "13.2.3", | ||
@@ -51,0 +51,0 @@ "sloc": "0.2.1", |
@@ -29,3 +29,3 @@ # nodemailer-mock | ||
* `nodemailerMock.mock.getSentMail()` | ||
* returns an array of sent emails | ||
* returns an array of sent emails during your tests, since the last reset | ||
* `nodemailerMock.mock.setShouldFailOnce()` | ||
@@ -38,3 +38,3 @@ * will return an error on the next call to `transport.sendMail()` | ||
* `nodemailerMock.mock.setShouldFailCheck({Function} (email)=>{})` | ||
* indicate if the specific email pass to the function should fail the call to `transport.sendMail()` | ||
* indicate if the specific email passed to the function should fail the call to `transport.sendMail()` | ||
* if function returns `true`, return error | ||
@@ -49,7 +49,8 @@ * if function returns `false`, return success | ||
* `nodemailerMock.mock.setFailResponse({Error} err)` | ||
* set the Error that is returned in the callback for `transport.sendMail()` | ||
* set the `Error` that is returned in the callback for `transport.sendMail()` | ||
>_Note that the `.mock` methods in previous versions are aliased to the new names._ | ||
>_Version 1.5+ returns an `Error` object on error rather than a string. | ||
>_Version 1.5+ returns an `Error` object on error rather than a string._ | ||
# usage | ||
@@ -56,0 +57,0 @@ The mocked module behaves in a similar fashion to other transports provided by `nodemailer`. |
@@ -18,12 +18,32 @@ 'use strict'; | ||
describe('nodestyle callback api', () => { | ||
it('should allow plugins to be added', (done) => { | ||
describe('nodemailer functionality', () => { | ||
it('should allow plugins to be added', async () => { | ||
// try to add a plugin, twice for coverage; | ||
transport.use('plugin-name', {}); | ||
transport.use('plugin-name', {}); | ||
transport.use('plugin-name', { foo: 'bar' }); | ||
let { 'plugin-name': plugins } = transport.mock.getPlugins(); | ||
let [ plugin ] = plugins; | ||
should(plugin).not.equal(undefined); | ||
plugin.should.have.property('foo'); | ||
plugin.foo.should.equal('bar'); | ||
transport.use('plugin-name', { foo: 'rab' }); | ||
plugins = transport.mock.getPlugins()['plugin-name']; | ||
[ , plugin ] = plugins; | ||
plugin.foo.should.equal('rab'); | ||
// allow for falsey step arg | ||
transport.use(false, {}); | ||
done(); | ||
transport.use(false, { foo: 'false' }); | ||
plugins = transport.mock.getPlugins()['']; | ||
[ plugin ] = plugins; | ||
plugin.foo.should.equal('false'); | ||
// make sure they get cleared | ||
nodemailer.mock.reset(); | ||
plugins = transport.mock.getPlugins()['']; | ||
should(plugins).equal(undefined); | ||
}); | ||
}) | ||
describe('nodestyle callback api', () => { | ||
it('should succeed for email sending', (done) => { | ||
@@ -30,0 +50,0 @@ // Send an email that should succeed |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36880
633
209