nodemailer-mock
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -46,168 +46,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
class NodemailerMocked { | ||
nodemailer; | ||
_mockedVerify = true; | ||
_mockedClose = true; | ||
// our response messages | ||
_successResponse = messages_1.messages.success_response; | ||
_failResponse = messages_1.messages.fail_response; | ||
_transporters = []; | ||
// Sent mail cache | ||
_sentMail = []; | ||
_closeCount = 0; | ||
// Should the callback be a success or failure? | ||
_shouldFail = false; | ||
_shouldFailOnce = false; | ||
_shouldFailCheck = null; | ||
_shouldUsePlugins = false; | ||
constructor(mailer) { | ||
this._mockedVerify = true; | ||
this._mockedClose = true; | ||
// our response messages | ||
this._successResponse = messages_1.messages.success_response; | ||
this._failResponse = messages_1.messages.fail_response; | ||
this._transporters = []; | ||
// Sent mail cache | ||
this._sentMail = []; | ||
this._closeCount = 0; | ||
// Should the callback be a success or failure? | ||
this._shouldFail = false; | ||
this._shouldFailOnce = false; | ||
this._shouldFailCheck = null; | ||
this._shouldUsePlugins = false; | ||
this.createTransport = (transport, options) => { | ||
// indicate that we are creating a transport | ||
debug('createTransport', options); | ||
const _transport = new NodemailerMockTransport(this, { | ||
addSentMail: (mail) => this._sentMail.push(mail), | ||
incrementCloseCallCount: () => this._closeCount++, | ||
}, transport, options); | ||
const _transporter = new NodemailerMockMail(this, _transport); | ||
_transport.nodemailer = _transporter; | ||
this._transporters.push(_transporter); | ||
return _transporter; | ||
}; | ||
this.mock = { | ||
/** | ||
* reset mock values to defaults | ||
*/ | ||
reset: () => { | ||
this._sentMail = []; | ||
this._closeCount = 0; | ||
this._shouldFail = this._shouldFailOnce = false; | ||
this._successResponse = messages_1.messages.success_response; | ||
this._failResponse = messages_1.messages.fail_response; | ||
this._mockedVerify = true; | ||
this._shouldFailCheck = null; | ||
this._shouldUsePlugins = false; | ||
}, | ||
/** | ||
* the transporters that have been created by this mock | ||
* @returns {Object[]} an array of transporters | ||
*/ | ||
getTransporters: () => this._transporters, | ||
/** | ||
* get an array of sent emails | ||
* @return {Object[]} an array of emails | ||
*/ | ||
getSentMail: () => this._sentMail, | ||
/** | ||
* get the number of times close() has been called | ||
* @return {number} the number of times close() has been called | ||
*/ | ||
getCloseCallCount: () => this._closeCount, | ||
/** | ||
* determine if sendMail() should return errors once then succeed | ||
*/ | ||
setShouldFailOnce: (isSet) => (this._shouldFail = this._shouldFailOnce = | ||
typeof isSet !== 'undefined' ? isSet : true), | ||
/** | ||
* determine if nodemailer should return errors once then succeed | ||
* @return {boolean} true if sendMail() should return errors once then succeed | ||
*/ | ||
isShouldFailOnce: () => this._shouldFailOnce, | ||
/** | ||
* determine if sendMail() should return errors | ||
* @param {boolean} isFail true will return errors, false will return successes | ||
*/ | ||
setShouldFail: (isFail) => { | ||
this._shouldFail = isFail; | ||
if (!isFail) { | ||
this._shouldFailOnce = false; | ||
} | ||
}, | ||
/** | ||
* determine if sendMail() should return errors | ||
* @return {boolean} true if sendMail() should return errors | ||
*/ | ||
isShouldFail: () => this._shouldFail, | ||
/** | ||
* determine if transport.verify() should be mocked or not | ||
* @param {boolean} isMocked if the function should be mocked | ||
*/ | ||
setMockedVerify: (isMocked) => (this._mockedVerify = isMocked), | ||
/** | ||
* determine if transport.verify() should be mocked or not | ||
* @return {boolean} true if the function should be mocked | ||
*/ | ||
isMockedVerify: () => this._mockedVerify, | ||
/** | ||
* determine if transport.close() should be mocked or not | ||
* @param {boolean} isMocked if the function should be mocked | ||
* @return {void} | ||
*/ | ||
setMockedClose: (isMocked) => (this._mockedClose = isMocked), | ||
/** | ||
* determine if transport.close() will be mocked or not | ||
* @return {boolean} true if the function will be mocked | ||
*/ | ||
isMockedClose: () => this._mockedClose, | ||
/** | ||
* set the response messages for successes | ||
* @param {string} success | ||
*/ | ||
setSuccessResponse: (success) => (this._successResponse = success), | ||
/** | ||
* get the response messages for successes | ||
* @return {string} success | ||
*/ | ||
getSuccessResponse: () => this._successResponse, | ||
/** | ||
* set the response messages for failures | ||
* @param {Error} failure | ||
*/ | ||
setFailResponse: (failure) => (this._failResponse = failure), | ||
/** | ||
* get the response messages for failures | ||
* @return {Error} failure | ||
*/ | ||
getFailResponse: () => this._failResponse, | ||
/** | ||
* set the check function that returns true if a message send should fail | ||
* @param {CheckMailMessageOrNull} check | ||
*/ | ||
setShouldFailCheck: (check) => { | ||
this._shouldFailCheck = check; | ||
}, | ||
/** | ||
* get the check function that returns true if a message send should fail | ||
* @return {CheckMailMessageOrNull} check | ||
* @return {boolean} true if the message should fail | ||
*/ | ||
getShouldFailCheck: () => this._shouldFailCheck, | ||
/** | ||
* schedule the mocked transports to be idle | ||
* @param timeout the time in ms to wait before setting the transport to idle | ||
*/ | ||
scheduleIsIdle: (isIdle, timeout) => { | ||
setTimeout(() => { | ||
// iterate over all transporters and set them to idle | ||
return this._transporters.forEach((transporter) => transporter.mock.setIdle(isIdle)); | ||
}, timeout); | ||
}, | ||
/** | ||
* set the mocked transports to be idle | ||
* @param isIdle true if the transport should be idle | ||
* @return {void} | ||
*/ | ||
setIsIdle: (isIdle) => { | ||
// iterate over all transporters and set them to idle | ||
return this._transporters.forEach((transporter) => transporter.mock.setIdle(isIdle)); | ||
}, | ||
/** | ||
* set if plugins should be used | ||
* @param {boolean} shouldUsePlugins | ||
* @return {void} | ||
*/ | ||
setUnmockedUsePlugins: (shouldUsePlugins) => (this._shouldUsePlugins = shouldUsePlugins), | ||
/** | ||
* get if plugins should be used | ||
* @return {boolean} true if plugins should be used | ||
*/ | ||
isUnmockedUsePlugins: () => this._shouldUsePlugins, | ||
}; | ||
this.nodemailer = mailer; | ||
@@ -225,9 +75,165 @@ // legacy aliases | ||
} | ||
createTransport = (transport, options) => { | ||
// indicate that we are creating a transport | ||
debug('createTransport', options); | ||
const _transport = new NodemailerMockTransport(this, { | ||
addSentMail: (mail) => this._sentMail.push(mail), | ||
incrementCloseCallCount: () => this._closeCount++, | ||
}, transport, options); | ||
const _transporter = new NodemailerMockMail(this, _transport); | ||
_transport.nodemailer = _transporter; | ||
this._transporters.push(_transporter); | ||
return _transporter; | ||
}; | ||
mock = { | ||
/** | ||
* reset mock values to defaults | ||
*/ | ||
reset: () => { | ||
this._sentMail = []; | ||
this._closeCount = 0; | ||
this._shouldFail = this._shouldFailOnce = false; | ||
this._successResponse = messages_1.messages.success_response; | ||
this._failResponse = messages_1.messages.fail_response; | ||
this._mockedVerify = true; | ||
this._shouldFailCheck = null; | ||
this._shouldUsePlugins = false; | ||
}, | ||
/** | ||
* the transporters that have been created by this mock | ||
* @returns {Object[]} an array of transporters | ||
*/ | ||
getTransporters: () => this._transporters, | ||
/** | ||
* get an array of sent emails | ||
* @return {Object[]} an array of emails | ||
*/ | ||
getSentMail: () => this._sentMail, | ||
/** | ||
* get the number of times close() has been called | ||
* @return {number} the number of times close() has been called | ||
*/ | ||
getCloseCallCount: () => this._closeCount, | ||
/** | ||
* determine if sendMail() should return errors once then succeed | ||
*/ | ||
setShouldFailOnce: (isSet) => (this._shouldFail = this._shouldFailOnce = | ||
typeof isSet !== 'undefined' ? isSet : true), | ||
/** | ||
* determine if nodemailer should return errors once then succeed | ||
* @return {boolean} true if sendMail() should return errors once then succeed | ||
*/ | ||
isShouldFailOnce: () => this._shouldFailOnce, | ||
/** | ||
* determine if sendMail() should return errors | ||
* @param {boolean} isFail true will return errors, false will return successes | ||
*/ | ||
setShouldFail: (isFail) => { | ||
this._shouldFail = isFail; | ||
if (!isFail) { | ||
this._shouldFailOnce = false; | ||
} | ||
}, | ||
/** | ||
* determine if sendMail() should return errors | ||
* @return {boolean} true if sendMail() should return errors | ||
*/ | ||
isShouldFail: () => this._shouldFail, | ||
/** | ||
* determine if transport.verify() should be mocked or not | ||
* @param {boolean} isMocked if the function should be mocked | ||
*/ | ||
setMockedVerify: (isMocked) => (this._mockedVerify = isMocked), | ||
/** | ||
* determine if transport.verify() should be mocked or not | ||
* @return {boolean} true if the function should be mocked | ||
*/ | ||
isMockedVerify: () => this._mockedVerify, | ||
/** | ||
* determine if transport.close() should be mocked or not | ||
* @param {boolean} isMocked if the function should be mocked | ||
* @return {void} | ||
*/ | ||
setMockedClose: (isMocked) => (this._mockedClose = isMocked), | ||
/** | ||
* determine if transport.close() will be mocked or not | ||
* @return {boolean} true if the function will be mocked | ||
*/ | ||
isMockedClose: () => this._mockedClose, | ||
/** | ||
* set the response messages for successes | ||
* @param {string} success | ||
*/ | ||
setSuccessResponse: (success) => (this._successResponse = success), | ||
/** | ||
* get the response messages for successes | ||
* @return {string} success | ||
*/ | ||
getSuccessResponse: () => this._successResponse, | ||
/** | ||
* set the response messages for failures | ||
* @param {Error} failure | ||
*/ | ||
setFailResponse: (failure) => (this._failResponse = failure), | ||
/** | ||
* get the response messages for failures | ||
* @return {Error} failure | ||
*/ | ||
getFailResponse: () => this._failResponse, | ||
/** | ||
* set the check function that returns true if a message send should fail | ||
* @param {CheckMailMessageOrNull} check | ||
*/ | ||
setShouldFailCheck: (check) => { | ||
this._shouldFailCheck = check; | ||
}, | ||
/** | ||
* get the check function that returns true if a message send should fail | ||
* @return {CheckMailMessageOrNull} check | ||
* @return {boolean} true if the message should fail | ||
*/ | ||
getShouldFailCheck: () => this._shouldFailCheck, | ||
/** | ||
* schedule the mocked transports to be idle | ||
* @param timeout the time in ms to wait before setting the transport to idle | ||
*/ | ||
scheduleIsIdle: (isIdle, timeout) => { | ||
setTimeout(() => { | ||
// iterate over all transporters and set them to idle | ||
return this._transporters.forEach((transporter) => transporter.mock.setIdle(isIdle)); | ||
}, timeout); | ||
}, | ||
/** | ||
* set the mocked transports to be idle | ||
* @param isIdle true if the transport should be idle | ||
* @return {void} | ||
*/ | ||
setIsIdle: (isIdle) => { | ||
// iterate over all transporters and set them to idle | ||
return this._transporters.forEach((transporter) => transporter.mock.setIdle(isIdle)); | ||
}, | ||
/** | ||
* set if plugins should be used | ||
* @param {boolean} shouldUsePlugins | ||
* @return {void} | ||
*/ | ||
setUnmockedUsePlugins: (shouldUsePlugins) => (this._shouldUsePlugins = shouldUsePlugins), | ||
/** | ||
* get if plugins should be used | ||
* @return {boolean} true if plugins should be used | ||
*/ | ||
isUnmockedUsePlugins: () => this._shouldUsePlugins, | ||
}; | ||
} | ||
class NodemailerMockTransport { | ||
nodemailermock; | ||
nodemailer; | ||
realmailer; | ||
name = 'Nodemailer Mock Transport'; | ||
version = '0.0.0'; | ||
// handlers passed from the mock to the transport | ||
handlers; | ||
_isIdle = false; | ||
_closeCallCount = 0; | ||
constructor(nodemailermock, handlers, transport, options) { | ||
this.name = 'Nodemailer Mock Transport'; | ||
this.version = '0.0.0'; | ||
this._isIdle = false; | ||
this._closeCallCount = 0; | ||
this.nodemailermock = nodemailermock; | ||
@@ -328,10 +334,13 @@ this.handlers = handlers; | ||
class NodemailerMockMail extends mailer_1.default { | ||
mock; | ||
nodemailermock; | ||
transporter; | ||
// transport plugins | ||
_userPluginsDefault = { | ||
compile: [], | ||
stream: [], | ||
}; | ||
_userPlugins = new Map(Object.entries(this._userPluginsDefault)); | ||
constructor(nodemailermock, transport) { | ||
super(transport); | ||
// transport plugins | ||
this._userPluginsDefault = { | ||
compile: [], | ||
stream: [], | ||
}; | ||
this._userPlugins = new Map(Object.entries(this._userPluginsDefault)); | ||
this.nodemailermock = nodemailermock; | ||
@@ -338,0 +347,0 @@ this.transporter = transport; |
{ | ||
"name": "nodemailer-mock", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Easy as pie nodemailer mock for unit testing your Node.js applications", | ||
"main": "./dist/nodemailer-mock.js", | ||
"repository": { | ||
@@ -32,5 +31,16 @@ "type": "git", | ||
"homepage": "https://github.com/doublesharp/nodemailer-mock#readme", | ||
"main": "./dist/nodemailer-mock.js", | ||
"module": "./dist/nodemailer-mock.mjs", | ||
"types": "./dist/nodemailer-mock.d.ts", | ||
"exports": { | ||
".": "./dist/nodemailer-mock.js", | ||
"./lib/messages": "./dist/lib/messages.js" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"prepare": "tsc", | ||
"clean": "shx rm -rf ./dist", | ||
"build": "npm run build:tsc && npm run build:mjs", | ||
"build:tsc": "tsc", | ||
"build:mjs": "cp ./mjs/nodemailer-mock.mjs ./dist/nodemailer-mock.mjs", | ||
"prepare": "npm run clean && npm run build", | ||
"pretest": "npm run build", | ||
"test": "mocha -r ts-node/register", | ||
@@ -41,5 +51,2 @@ "coverage-report": "nyc npm run test", | ||
"lint": "eslint . --ext .ts", | ||
"sloc": "sloc src test", | ||
"sloc-app": "sloc src", | ||
"sloc-test": "sloc test", | ||
"inspect": "NODE_ENV=test npm run test -- -s 0 --inspect --debug-brk", | ||
@@ -65,6 +72,6 @@ "examples": "npm run example:jest && npm run example:mocha", | ||
"@types/mockery": "^1.4.33", | ||
"@types/node": "^20.10.5", | ||
"@types/node": "^20.10.6", | ||
"@types/nodemailer": "^6.x", | ||
"@typescript-eslint/eslint-plugin": "^6.15.0", | ||
"@typescript-eslint/parser": "^6.15.0", | ||
"@typescript-eslint/eslint-plugin": "^6.17.0", | ||
"@typescript-eslint/parser": "^6.17.0", | ||
"chai": "^4.3.10", | ||
@@ -77,3 +84,3 @@ "eslint": "^8.56.0", | ||
"nyc": "^15.1.0", | ||
"sloc": "^0.3.0", | ||
"shx": "^0.3.4", | ||
"spec-xunit-file": "^0.0.1-3", | ||
@@ -80,0 +87,0 @@ "ts-jest": "^29.1.1", |
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
9
780
46992