GithubHelp home page GithubHelp logo

cerkiewny / nodemailer-stub Goto Github PK

View Code? Open in Web Editor NEW

This project forked from limedeck/nodemailer-stub

0.0 1.0 0.0 53 KB

✉️️ Stub transport for Nodemailer. Testing your mails in Node.js is now easy.

License: ISC License

JavaScript 100.00%

nodemailer-stub's Introduction

nodemailer-stub

Build Status Coverage Status npm GitHub release

Nodemailer-stub comes with a stub transport for Nodemailer. The Stub stores the messages in memory but mimics real mail behaviour. It also contains a smart testing class called InteractsWithMail, which allows users to access, read, count and flush the messages in memory in their testing environment.

Installation

$ yarn add nodemailer-stub -D
#... or via npm
$ npm install nodemailer-stub --save-dev

Usage

This is an example use case for the Stub.

import { stubTransport } from 'nodemailer-stub'
import nodeMailer from 'nodemailer'

let transport = nodeMailer.createTransport(stubTransport)

let mail = await transport.sendMail({
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Nodemailer stub works!',
  text: 'Wohoo'
})

For testing purposes, there is also a transport called errorTransport, where the transport throws an error during execution, to help with testing the robustness of your mail service logic.

We've also included a testing utility class, called interactsWithMail. You can use it in your tests like this:

import { interactsWithMail as iwm } from 'nodemailer-stub'

const exampleMail = {
  to: '[email protected]',
  from: '[email protected]',
  subject: 'testing',
  content: 'foo',
  contents: ['foo'],
  contentType: 'text/plain'
}

test('it retrieves the last message', () => {
  iwm.newMail(exampleMail)

  let lastMail = iwm.lastMail()

  lastMail.to.should.eq('[email protected]')
  lastMail.from.should.eq('[email protected]')
  lastMail.subject.should.eq('testing')
  lastMail.content.should.eq(['foo'])
  lastMail.contents.should.eq(['foo'])
  lastMail.contentType.should.eq('text/plain')
})

Available methods for interactsWithMail:

lastMail()

Retrieves last mail. Accessible properties:

  • from
  • to
  • subject
  • content
  • contents
  • contentType

newMail (Object)

Adds a new mail to the list of all mails.

Available properties:

  • from (required)
  • to (required)
  • subject
  • text (required)

flushMails ()

Flushes all messages. Useful when testing multiple occurrences of mailer, and should be used in afterAll or afterEach hooks in your tests.

sentMailsCount ()

Retrieves a count of how many emails were sent in the last mailer call.

Testing

All tests can be executed with the following command:

$ yarn test

License

See LICENSE file.

nodemailer-stub's People

Contributors

jakubhomoly avatar jeffwilcox avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.