smtp-tester
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -19,3 +19,3 @@ const path = require('path'); | ||
smtpServer.listen(port, false); | ||
smtpServer.listen(port); | ||
@@ -37,2 +37,21 @@ const smtpTester = { | ||
}, | ||
captureOne(recipient, { wait } = {}) { | ||
return new Promise(function(resolve, reject) { | ||
function handler(address, id, email) { | ||
smtpTester.unbind(recipient, handler); | ||
smtpTester.remove(id); | ||
resolve({ address, id, email }); | ||
} | ||
smtpTester.bind(recipient, handler); | ||
if (wait > 0) { | ||
setTimeout(function() { | ||
smtpTester.unbind(recipient, handler); | ||
const error = new Error(`No message delivered to ${recipient}`); | ||
reject(error); | ||
}, wait); | ||
} | ||
}); | ||
}, | ||
remove(messageID) { | ||
@@ -113,3 +132,4 @@ if (messageID) | ||
headers, | ||
body: parsedEmail.text | ||
body: parsedEmail.text, | ||
html: parsedEmail.html | ||
}; | ||
@@ -116,0 +136,0 @@ |
{ | ||
"name": "smtp-tester", | ||
"description": "Quick and dirty smtp server, that accepts handlers to process messages", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"url": "http://github.com/deitch/smtp-tester", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -57,2 +57,37 @@ smtp-tester | ||
If you intend to capture a single message using promises, you can do: | ||
```js | ||
mailServer.captureOne('foo@bar.com') | ||
.then(function({ address, id, email }) { | ||
// Do something interesting | ||
}); | ||
``` | ||
Most likely you'll want to use the `wait` option as well, so if no message is received | ||
in the given time frame, `captureOne()` rejects the promise: | ||
```js | ||
mailServer.captureOne('foo@bar.com', { wait: 1000 }) | ||
.then(function({ address, id, email }) { | ||
// Do something interesting | ||
}) | ||
.catch(function(error) { | ||
// No message delivered to foo@bar.com in 1 second. | ||
}); | ||
``` | ||
Now using async/await: | ||
```js | ||
try { | ||
const { email } = await mailServer.captureOne('foo@bar.com', { wait: 1000 }); | ||
} catch (error) { | ||
// No message delivered to foo@bar.com in 1 second. | ||
} | ||
``` | ||
This is useful for testing that _no_ message was delivered, too. | ||
# Catch-All Handlers | ||
@@ -115,3 +150,3 @@ If you want a handler to catch every email that is sent through the system, just bind with no address at all. | ||
* id: Internal ID of the email in this mail server process. Useful for removing messages or checking against something in our cache. | ||
* email: JavaScript object of the email, containing "sender", "receivers", "data" (raw text), "headers" and "body". | ||
* email: JavaScript object of the email, containing "sender", "receivers", "data" (raw text), "headers", "body" (plain text) and "html". | ||
@@ -122,3 +157,3 @@ Sample email object is as follows, taken from the test.js included with the package. | ||
{ | ||
sender: 'mailtest@bar.com, | ||
sender: 'mailtest@bar.com', | ||
receivers: { | ||
@@ -125,0 +160,0 @@ 'foo@bar.com': true |
@@ -23,2 +23,3 @@ const Nodemailer = require('nodemailer'); | ||
test('setup', function(t) { | ||
@@ -287,2 +288,58 @@ const port = 4025; | ||
test('captureOne', function(t) { | ||
const recipient = 'foo@gmail.com'; | ||
const subject = 'email test'; | ||
const body = 'This is a test email'; | ||
smtpTester.removeAll(); | ||
return sendmail(recipient, subject, body) | ||
.then(() => smtpTester.captureOne(recipient)) | ||
.then(function({ id, email }) { | ||
t.ok(id); | ||
t.equal(email.body, body); | ||
}); | ||
}); | ||
test('captureOne with wait', function(t) { | ||
smtpTester.removeAll(); | ||
return smtpTester.captureOne('foo@gmail.com', { wait: 100 }) | ||
.then(function() { | ||
t.fail('Expected promise to be rejected'); | ||
}) | ||
.catch(function(error) { | ||
t.equal(error.message, 'No message delivered to foo@gmail.com'); | ||
}); | ||
}); | ||
test('html', function(t) { | ||
smtpTester.removeAll(); | ||
const recipient = 'foo@gmail.com'; | ||
const subject = 'email test'; | ||
const html = '<h1>This is a test email</h1>'; | ||
function handler(address, id, email) { | ||
t.equal(address, recipient); | ||
t.equal(email.html, html); | ||
smtpTester.unbind(recipient, handler); | ||
t.end(); | ||
} | ||
smtpTester.bind(recipient, handler); | ||
smtpTransport.sendMail({ | ||
from: sender, | ||
to: recipient, | ||
subject, | ||
html | ||
}).catch(t.error); | ||
}); | ||
test('teardown', function(t) { | ||
@@ -289,0 +346,0 @@ smtpTester.stop(); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25681
527
205
0