https://github.com/limedeck/nodemailer-stub
✉️️ Stub transport for Nodemailer. Testing your mails in Node.js is now easy.
https://github.com/limedeck/nodemailer-stub
nodejs nodemailer nodemailer-transport testing
Last synced: 2 months ago
JSON representation
✉️️ Stub transport for Nodemailer. Testing your mails in Node.js is now easy.
- Host: GitHub
- URL: https://github.com/limedeck/nodemailer-stub
- Owner: LimeDeck
- License: isc
- Created: 2017-02-13T12:01:39.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T17:30:06.000Z (over 2 years ago)
- Last Synced: 2025-04-09T03:25:42.656Z (2 months ago)
- Topics: nodejs, nodemailer, nodemailer-transport, testing
- Language: JavaScript
- Homepage:
- Size: 456 KB
- Stars: 10
- Watchers: 4
- Forks: 4
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nodemailer-stub
[](https://travis-ci.org/LimeDeck/nodemailer-stub)
[](https://coveralls.io/github/LimeDeck/nodemailer-stub?branch=master)
[](https://www.npmjs.com/package/nodemailer-stub)
[]()Nodemailer-stub comes with a stub transport for [Nodemailer](https://github.com/nodemailer/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
```console
$ yarn add nodemailer-stub -D
#... or via npm
$ npm install nodemailer-stub --save-dev
```## Usage
This is an example use case for the Stub.```javascript
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:
```javascript
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:```console
$ yarn test
```## License
See LICENSE file.