├── .coveralls.yml ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── index.js ├── package.json └── test └── test-mock-transport.js /.coveralls.yml: -------------------------------------------------------------------------------- 1 | repo_token: 4xlYo3NSUTrNyfBkUtUHJZ0m3owzYOPbC 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .nyc_output 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.11" 5 | - "iojs" 6 | after_script: "nyc report --reporter=text-lcov | ./node_modules/coveralls/bin/coveralls.js" 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | 6 | # [1.3.0](https://www.github.com/bcoe/nodemailer-mock-transport/compare/v1.2.0...v1.3.0) (2016-06-03) 7 | 8 | 9 | ### Features 10 | 11 | * allow "to" to be an array of email addresses (#5) ([ca277d7](https://www.github.com/bcoe/nodemailer-mock-transport/commit/ca277d7)) 12 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Contributors 2 | 3 | Permission to use, copy, modify, and/or distribute this software 4 | for any purpose with or without fee is hereby granted, provided 5 | that the above copyright notice and this permission notice 6 | appear in all copies. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 10 | OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE 11 | LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES 12 | OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 13 | WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 14 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nodemailer-mock-transport 2 | 3 | [![Build Status](https://travis-ci.org/bcoe/nodemailer-mock-transport.png)](https://travis-ci.org/bcoe/nodemailer-mock-transport) 4 | [![Coverage Status](https://coveralls.io/repos/bcoe/nodemailer-mock-transport/badge.svg?branch=)](https://coveralls.io/r/bcoe/nodemailer-mock-transport?branch=) 5 | 6 | Mock nodemailer-transport, for testing services that rely on nodemailer. 7 | 8 | ```js 9 | describe('mock-transport', function() { 10 | it('should store configuration options so that they can be asserted against', function() { 11 | var transport = mockTransport({ 12 | foo: 'bar' 13 | }); 14 | transport.options.foo.should.equal('bar'); 15 | }); 16 | 17 | it('should store emails sent with nodemailer, so that they can be asserted against', function() { 18 | var transport = mockTransport({ 19 | foo: 'bar' 20 | }); 21 | 22 | var transporter = nodemailer.createTransport(transport); 23 | 24 | transporter.sendMail({ 25 | from: 'sender@address', 26 | to: 'receiver@address', 27 | subject: 'hello', 28 | text: 'hello world!' 29 | }); 30 | 31 | transport.sentMail.length.should.equal(1); 32 | transport.sentMail[0].data.to.should.equal('receiver@address'); 33 | transport.sentMail[0].message.content.should.equal('hello world!'); 34 | }); 35 | 36 | it('should return an error and not send an email if there is no `to` in the mail data object', function () { 37 | var transport = mockTransport({ 38 | foo: 'bar' 39 | }); 40 | 41 | var transporter = nodemailer.createTransport(transport); 42 | 43 | transporter.sendMail({ 44 | from: 'sender@address', 45 | subject: 'hello', 46 | text: 'hello world!' 47 | }); 48 | 49 | transport.sentMail.length.should.equal(0); 50 | }); 51 | }); 52 | ``` 53 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var packageData = require('./package.json') 2 | var userValidate = require('npm-user-validate') 3 | 4 | var MockTransport = function (options) { 5 | this.options = options || {} 6 | this.sentMail = [] 7 | this.name = 'Mock' 8 | this.version = packageData.version 9 | } 10 | 11 | function validate (addr) { 12 | try { 13 | var err = userValidate.email(addr) 14 | if (err != null) return err 15 | } catch (_err) { 16 | return new Error('Error validating address.') 17 | } 18 | return null 19 | } 20 | 21 | MockTransport.prototype.send = function (mail, callback) { 22 | var err 23 | 24 | if (!mail.data.to) { 25 | return callback(new Error('I need to know who this email is being sent to :-(')) 26 | } 27 | 28 | if (Array.isArray(mail.data.to)) { 29 | for (var i = 0; i < mail.data.to.length; i++) { 30 | var addr = mail.data.to[i] 31 | err = validate(addr) 32 | if (err != null) { 33 | return callback(err) 34 | } 35 | } 36 | } else { 37 | err = validate(mail.data.to) 38 | if (err != null) { 39 | return callback(err) 40 | } 41 | } 42 | 43 | this.sentMail.push(mail) 44 | return callback() 45 | } 46 | 47 | module.exports = function (options) { 48 | return new MockTransport(options) 49 | } 50 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodemailer-mock-transport", 3 | "version": "1.3.0", 4 | "description": "mock-mailer for putting tests around services that use node-mailer", 5 | "main": "index.js", 6 | "scripts": { 7 | "pretest": "standard", 8 | "test": "nyc mocha", 9 | "release": "standard-version" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://www.github.com/bcoe/nodemailer-mock-transport" 14 | }, 15 | "keywords": [ 16 | "nodemailer", 17 | "test", 18 | "mock" 19 | ], 20 | "author": "Ben Coe ", 21 | "license": "ISC", 22 | "devDependencies": { 23 | "chai": "^3.5.0", 24 | "coveralls": "^2.11.2", 25 | "mocha": "^2.1.0", 26 | "nodemailer": "^2.4.2", 27 | "nyc": "^8.3.0", 28 | "standard": "^7.1.1", 29 | "standard-version": "^3.0.0" 30 | }, 31 | "dependencies": { 32 | "npm-user-validate": "^0.1.2" 33 | } 34 | } -------------------------------------------------------------------------------- /test/test-mock-transport.js: -------------------------------------------------------------------------------- 1 | /* global it, describe */ 2 | 3 | var nodemailer = require('nodemailer') 4 | var mockTransport = require('../index') 5 | 6 | require('chai').should() 7 | 8 | describe('mock-transport', function () { 9 | it('should store configuration options so that they can be asserted against', function () { 10 | var transport = mockTransport({ 11 | foo: 'bar' 12 | }) 13 | transport.options.foo.should.equal('bar') 14 | }) 15 | 16 | it('should store emails sent with nodemailer, so that they can be asserted against', function () { 17 | var transport = mockTransport({ 18 | foo: 'bar' 19 | }) 20 | 21 | var transporter = nodemailer.createTransport(transport) 22 | 23 | transporter.sendMail({ 24 | from: 'sender@address.com', 25 | to: 'receiver@address.com', 26 | subject: 'hello', 27 | text: 'hello world!' 28 | }) 29 | 30 | transport.sentMail.length.should.equal(1) 31 | transport.sentMail[0].data.to.should.equal('receiver@address.com') 32 | transport.sentMail[0].message.content.should.equal('hello world!') 33 | }) 34 | 35 | it('should return an error and not send an email if there is no `to` in the mail data object', function () { 36 | var transport = mockTransport({ 37 | foo: 'bar' 38 | }) 39 | 40 | var transporter = nodemailer.createTransport(transport) 41 | 42 | transporter.sendMail({ 43 | from: 'sender@address.com', 44 | subject: 'hello', 45 | text: 'hello world!' 46 | }) 47 | 48 | transport.sentMail.length.should.equal(0) 49 | }) 50 | 51 | it('should return an error and not send an email if the `to` email address is invalid', function () { 52 | var transport = mockTransport({ 53 | foo: 'bar' 54 | }) 55 | 56 | var transporter = nodemailer.createTransport(transport) 57 | 58 | transporter.sendMail({ 59 | to: 'lolbad@email', 60 | from: 'sender@address.com', 61 | subject: 'hello', 62 | text: 'hello world!' 63 | }) 64 | 65 | transport.sentMail.length.should.equal(0) 66 | }) 67 | 68 | it('should allow "to" to be an array of addresses', function () { 69 | var transport = mockTransport({ 70 | foo: 'bar' 71 | }) 72 | var transporter = nodemailer.createTransport(transport) 73 | var to = ['receiver@address.com', 'receiver2@address.com'] 74 | transporter.sendMail({ 75 | from: 'sender@address.com', 76 | to: to, 77 | subject: 'hello', 78 | text: 'hello world!' 79 | }) 80 | transport.sentMail.length.should.equal(1) 81 | transport.sentMail[0].data.to.should.eql(to) 82 | transport.sentMail[0].message.content.should.equal('hello world!') 83 | }) 84 | 85 | it('should not send an email if "to" is array and element isn\'t valid', function () { 86 | var transport = mockTransport({ 87 | foo: 'bar' 88 | }) 89 | var transporter = nodemailer.createTransport(transport) 90 | var to = ['receiver@address.com', 34] 91 | transporter.sendMail({ 92 | from: 'sender@address.com', 93 | to: to, 94 | subject: 'hello', 95 | text: 'hello world!' 96 | }) 97 | transport.sentMail.length.should.equal(0) 98 | }) 99 | }) 100 | --------------------------------------------------------------------------------