nodemailer-mock
Advanced tools
Comparing version 1.5.0 to 1.5.1
@@ -27,6 +27,11 @@ 'use strict'; | ||
let shouldFailOnce = false; | ||
let shouldFailCheck = null; | ||
// Determine if the test should return success or failure | ||
const determineResponseSuccess = function determineResponseSuccess() { | ||
const determineResponseSuccess = function determineResponseSuccess(email) { | ||
return new Promise((resolve, reject) => { | ||
// if shouldFailCheck defined use it | ||
if (email && shouldFailCheck && shouldFailCheck(email)) { | ||
return reject(new Error('nodemailer-mock fail response')); | ||
} | ||
// determine if we want to return an error | ||
@@ -60,3 +65,3 @@ if (shouldFail) { | ||
const info = messages.info(); | ||
return determineResponseSuccess() | ||
return determineResponseSuccess(email) | ||
.then(() => { | ||
@@ -163,2 +168,10 @@ // Resolve/Success | ||
/** | ||
* set the check function that returns true if a message send should fail | ||
* @param {function} check | ||
* * */ | ||
setShouldFailCheck: (check) => { | ||
shouldFailCheck = check; | ||
}, | ||
/** | ||
* get an array of sent emails | ||
@@ -178,2 +191,3 @@ * @return {Object[]} an array of emails | ||
mockedVerify = true; | ||
shouldFailCheck = null; | ||
}, | ||
@@ -190,2 +204,3 @@ }; | ||
sentMail: mock.getSentMail, | ||
shouldFailCheck: mock.shouldFailCheck, | ||
}); | ||
@@ -192,0 +207,0 @@ |
{ | ||
"name": "nodemailer-mock", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"description": "Mock nodemailer module for testing", | ||
@@ -5,0 +5,0 @@ "main": "nodemailer-mock.js", |
@@ -32,16 +32,21 @@ # nodemailer-mock | ||
* will return an error on the next call to `transport.sendMail()` | ||
* `nodemailerMock.mock.setShouldFail(true|false)` | ||
* `nodemailerMock.mock.setShouldFail({boolean} shouldFail)` | ||
* indicate if errors should be returned for subsequent calls to `transport.sendMail()` | ||
* if `true`, return error | ||
* if `false`, return success | ||
* `nodemailerMock.mock.setMockedVerify(true|false)` | ||
* `nodemailerMock.mock.setShouldFailCheck({Function} (email)=>{})` | ||
* indicate if the specific email pass to the function should fail the call to `transport.sendMail()` | ||
* if function returns `true`, return error | ||
* if function returns `false`, return success | ||
* `nodemailerMock.mock.setMockedVerify({boolean} isMocked)` | ||
* determine if a call to `transport.verify()` should be mocked or passed through to `nodemailer` | ||
* if `true`, use a mocked callback | ||
* if `false`, pass through to a real `nodemailer` transport | ||
* `nodemailerMock.mock.setSuccessResponse(success)` | ||
* `nodemailerMock.mock.setSuccessResponse({Mixed} success)` | ||
* set the success message that is returned in the callback for `transport.sendMail()` | ||
* `nodemailerMock.mock.setFailResponse(err)` | ||
* set the err that is returned in the callback for `transport.sendMail()` | ||
* `nodemailerMock.mock.setFailResponse({Error} err)` | ||
* 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. | ||
@@ -181,3 +186,3 @@ # usage | ||
// tell the mock class to return an error | ||
const err = 'My custom error'; | ||
const err = new Error('My custom error'); | ||
nodemailerMock.mock.setShouldFailOnce(); | ||
@@ -184,0 +189,0 @@ nodemailerMock.mock.setFailResponse(err); |
@@ -99,2 +99,23 @@ 'use strict'; | ||
it('should fail if shouldFailCheck returns true for message', (done) => { | ||
// Tell the mock to fail once then succeed | ||
// (for testing retries, or so you dont have to reset a test) | ||
nodemailer.mock.setShouldFailCheck((email) => { | ||
return email === 'FailMe'; | ||
}); | ||
// Send an email that should fail | ||
transport.sendMail('FailMe', (err1, info1) => { | ||
should(err1).equal(messages.fail_response); | ||
info1.response.should.equal(messages.fail_response); | ||
// Send an email that should succeed | ||
transport.sendMail('Email', (err2, info2) => { | ||
should(err2).equal(null); | ||
info2.response.should.equal(messages.success_response); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should have a custom success message', (done) => { | ||
@@ -221,2 +242,21 @@ // This is the success message we want it to return | ||
it('should fail if shouldFailCheck returns true for message', (done) => { | ||
// Tell the mock to fail once then succeed | ||
// (for testing retries, or so you dont have to reset a test) | ||
nodemailer.mock.setShouldFailCheck((email) => { | ||
return email === 'FailMe'; | ||
}); | ||
// Send an email that should fail | ||
transport.sendMail('FailMe') | ||
.catch((err) => { | ||
should(err.message).equal('nodemailer-mock failure'); | ||
transport.sendMail('Email') | ||
.then((info) => { | ||
info.response.should.equal(messages.success_response); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should have a custom success message', (done) => { | ||
@@ -346,2 +386,26 @@ // This is the success message we want it to return | ||
it('should fail if shouldFailCheck returns true for message', async () => { | ||
// Tell the mock to fail once then succeed | ||
// (for testing retries, or so you dont have to reset a test) | ||
nodemailer.mock.setShouldFailCheck((email) => { | ||
return email === 'FailMe'; | ||
}); | ||
// Send an email that should fail | ||
try { | ||
await transport.sendMail('FailMe'); | ||
throw new Error(); // this should not happen | ||
} catch (err) { | ||
should(err.message).equal('nodemailer-mock failure'); | ||
} | ||
// This email should succeed | ||
try { | ||
const info = await transport.sendMail('Email'); | ||
info.response.should.equal(messages.success_response); | ||
} catch (err) { | ||
should(err).equal(null); | ||
} | ||
}); | ||
it('should have a custom success message', async () => { | ||
@@ -348,0 +412,0 @@ // This is the success message we want it to return |
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
35763
609
208