Socket
Socket
Sign inDemoInstall

nodemailer-mock

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nodemailer-mock - npm Package Compare versions

Comparing version 1.5.12 to 2.0.0

.mocharc.json

11

examples/__mocks__/nodemailer.js

@@ -0,3 +1,10 @@

/* eslint-disable @typescript-eslint/no-var-requires */
'use strict';
// const nodemailermock = require('../../dist/nodemailer-mock');
const { getMockFor, nodemailerLoading } = require('../../dist/nodemailer-mock');
const nodemailer = require('nodemailer');
const nodemailerMock = require('../../nodemailer-mock').getMockFor(nodemailer);
module.exports = nodemailerMock;
// if (nodemailerLoading) {
module.exports = getMockFor(nodemailer);
// }
// module.exports = nodemailer;

6

examples/mailer-test.js

@@ -1,4 +0,4 @@

/**
* This is a test module used in the Jest test example
*/
/* eslint-disable @typescript-eslint/no-var-requires */
'use strict';
const nodemailer = require('nodemailer');

@@ -5,0 +5,0 @@

{
"name": "nodemailer-mock",
"version": "1.5.12",
"version": "2.0.0",
"description": "Easy as pie nodemailer mock for unit testing your Node.js applications",
"main": "nodemailer-mock.js",
"directories": {
"lib": "./lib",
"test": "./test"
},
"scripts": {
"test": "mocha --exit",
"coverage-report": "./test/bin/nyc-coverage-report",
"coverage": "npm run coverage-report && open ./coverage/lcov-report/index.html",
"coverage-badge": "istanbul-cobertura-badger -e 95 -g 85 -b \"${PWD##*/}\"",
"lint": "eslint .",
"validate": "./test/bin/module-validator",
"depcheck": "./test/bin/dependency-checker",
"sloc": "sloc *.js lib test",
"sloc-app": "sloc *.js lib",
"sloc-test": "sloc test",
"inspect": "NODE_ENV=test mocha -s 0 --inspect --debug-brk",
"example:jest": "jest examples",
"example:mocha": "mocha examples/test"
},
"main": "./dist/nodemailer-mock.js",
"repository": {

@@ -39,3 +20,4 @@ "type": "git",

"mocha",
"jest"
"jest",
"typescript"
],

@@ -48,27 +30,52 @@ "author": "Justin Silver",

"homepage": "https://github.com/doublesharp/nodemailer-mock#readme",
"devDependencies": {
"colors": "^1.4.0",
"depcheck": "^1.4.3",
"eslint": "^8.13.0",
"istanbul-cobertura-badger": "^1.3.1",
"mocha": "^9.2.2",
"nyc": "^15.1.0",
"should": "^13.2.3",
"sloc": "^0.0.4",
"spec-xunit-file": "^0.0.1-3"
"scripts": {
"build": "tsc",
"prepare": "tsc",
"test": "mocha -r ts-node/register",
"coverage-report": "nyc npm run test",
"coverage": "npm run coverage-report && open ./coverage/index.html",
"coverage-badge": "istanbul-cobertura-badger -e 95 -g 85 -b \"${PWD##*/}\"",
"lint": "eslint . --ext .ts",
"sloc": "sloc src test",
"sloc-app": "sloc src",
"sloc-test": "sloc test",
"inspect": "NODE_ENV=test npm run test -- -s 0 --inspect --debug-brk",
"examples": "npm run example:jest && npm run example:mocha",
"example:jest": "jest examples",
"example:mocha": "cd examples; mocha -r ts-node/register"
},
"dependencies": {
"debug": "^4.3.4",
"debug": "^4.3.4"
},
"peerDependencies": {
"nodemailer": "^6.x"
},
"optionalDevDependencies": {
"jest": "^26.6.3",
"mockery": "^2.1.0"
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.4",
"@types/debug": "^4.1.7",
"@types/expect": "^24.3.0",
"@types/jest": "^29.4.0",
"@types/mocha": "^10.0.1",
"@types/mockery": "^1.4.30",
"@types/nodemailer": "^6.x",
"@typescript-eslint/eslint-plugin": "^5.49.0",
"@typescript-eslint/parser": "^5.49.0",
"chai": "^4.3.7",
"eslint": "^8.32.0",
"istanbul-cobertura-badger": "^1.3.1",
"jest": "^29.4.1",
"mocha": "^10.2.0",
"mockery": "^2.1.0",
"nyc": "^15.1.0",
"sloc": "^0.1.11",
"spec-xunit-file": "^0.0.1-3",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"pre-commit": [
"lint",
"validate",
"depcheck"
"lint"
],
"pre-push": []
}

@@ -27,36 +27,72 @@ # nodemailer-mock

* `nodemailerMock.mock.reset()`
* resets the mock class to default values
* `nodemailerMock.mock.getSentMail()`
* returns an array of sent emails during your tests, since the last reset
* `nodemailerMock.mock.setShouldFailOnce()`
* will return an error on the next call to `transport.sendMail()`
* `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.setShouldFailCheck({Function} (email)=>{})`
* indicate if the specific email passed 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({Mixed} success)`
* set the success message 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()`
## `NodemailerMock.mock` functions
>_Note that the `.mock` methods in previous versions are aliased to the new names._
- `reset: () => void`
- resets the mock class to default values
- `getSentMail: () => Mail.Options[]`
- returns an array of sent emails during your tests, since the last reset
- `getCloseCallCount: () => number`
- get the number of times the `transporter.close()` was called across all instances
- `setShouldFailOnce: (isSet?: boolean) => void`
- should the mock return an error on the next call to `transporter.sendMail()` or `transport.send()`
- `isShouldFailOnce: () => boolean`
- returns status of `setShouldFailOnce(?)`
- `setShouldFail: (isFail?: boolean) => void`
- indicate if errors should be returned for subsequent calls to `transporter.sendMail()` or `transport.send()`
- if `true`, return error
- if `false`, return success
- `isShouldFail: () => boolean`
- returns status of `setShouldFail()`
- `setShouldFailCheck: (check: CheckMailMessageOrNull) => void`
- indicate if the specific email passed to the function should fail the call to `transporter.sendMail()` or `transport.send()`
- if function returns `true`, return error
- if function returns `false`, return success
- use `type CheckMailMessageOrNull = ((email: MailMessage) => boolean) | null`
- `getShouldFailCheck: () => CheckMailMessageOrNull`
- returns the function used to check the `MailMessage` or `null` if it is not set
- `setMockedVerify: (isMocked: boolean) => void`
- determine if a call to `transport.verify()` should be mocked or passed through to `nodemailer`, defaults to `true`.
- if `true`, use a mocked callback
- if `false`, pass through to a real `nodemailer` transport
- `isMockedVerify: () => boolean`
- returns status of `setMockedVerify(?)`
- `setMockedClose: (isMocked: boolean) => void`
- determine if calls to `transporter.close()` should be passed through to the underlying transport, defaults to `true`.
- `isMockedClose: () => boolean`
- when the result is `true` the underlying transport is not used, when `false` the call is passed through.
- `setSuccessResponse: (response: string) => void`
- set the success message that is returned in the callback for `transporter.sendMail()` or `transport.send()`
- `getSuccessResponse: () => string`
- returns the success message value
- `setFailResponse: (error: Error) => void`
- set the `Error` that is returned in the callback for `transporter.sendMail()` or `transport.send()`
- `getFailResponse: () => Error`
- returns the fail `Error` value
- `scheduleIsIdle: (isIdle: boolean, timeout: number) => void`
- schedule a status change for calls to `transporter.isIdle()` instances
- `setIsIdle: (isIdle: boolean) => void`
- set the status that is returned by calls to all `transporter.isIdle()` instances
- `setUnmockedUsePlugins: (isUnmockUsePlugins: boolean) => void` default `false`
- should the plugins added via `transporter.use()` be run outside the mock?
- `isUnmockedUsePlugins: () => boolean`
- returns the status of `setUnmockedUsePlugins(?)`
>_Version 1.5+ returns an `Error` object on error rather than a string._
## `NodemailerMocktransporter.mock` functions
- `getPlugins: () => { [key: string]: Mail.PluginFunction<Mail.Options>[] }`
- returns the plugins that have been added via `transporter.use()` as arrays, keyed by step
- `getCloseCallCount: () => number`
- get the number of times `close()` has been called on this transporter. this number is not reset with the mock.
- `setIdle(isIdle: boolean): void`
- sets the idle state of `transporter.isIdle()` and emits an `idle` event when the `isIdle` argument is `true`.
# usage
The mocked module behaves in a similar fashion to other transports provided by `nodemailer`.
**setup test**
```javascript
const nodemailerMock = require('nodemailer-mock');
const transport = nodemailerMock.createTransport();
const nodemailermock = require('nodemailer-mock');
const transport = nodemailermock.createTransport();

@@ -68,2 +104,3 @@ // the email you want to send

**use nodestyle callbacks**
```javascript

@@ -88,2 +125,3 @@ // send with nodestyle callback

**use promises**
```javascript

@@ -110,2 +148,3 @@ // send with promises

**use async/await**
```javascript

@@ -126,5 +165,6 @@ // send an email with async / wait

```
# example tests using mocked module
To use `nodemailer-mock` in your tests you will need to mock `nodemailer` with it. There are working examples using [`jest`](https://www.npmjs.com/package/jest) and [`jest`](https://www.npmjs.com/package/mocha) in the [`./examples/`](https://github.com/doublesharp/nodemailer-mock/tree/master/examples) folder of the project. The `jest` code is in `./examples/__mocks__` and `./examples/__tests__`, and the `mocha` tests are in `./examples/test`. You will need to `npm i -D jest` and/or `npm i -D mockery` to run the examples, and with a shortcut of `npm run example:jest` and `npm run example:mocha`.
To use `nodemailer-mock` in your tests you will need to mock `nodemailer` with it. There are working examples using [`jest`](https://www.npmjs.com/package/jest) and [`jest`](https://www.npmjs.com/package/mocha) in the [`./examples/`](https://github.com/doublesharp/nodemailer-mock/tree/master/examples) folder of the project. The `jest` code is in `./examples/__mocks__` and `./examples/__tests__`, and the `mocha` tests are in `./examples/test`. Run the examples with `npm run example:jest` and `npm run example:mocha`. Both JavaScript and TypeScript example tests are provided.

@@ -136,5 +176,5 @@ ## example using jest

```javascript
/**
/**
* Jest Mock
* ./__mocks__/nodemailer.js
* ./__mocks__/nodemailer.js
**/

@@ -144,5 +184,5 @@ // load the real nodemailer

// pass it in when creating the mock using getMockFor()
const nodemailerMock = require('nodemailer-mock').getMockFor(nodemailer);
const nodemailermock = require('nodemailer-mock').getMockFor(nodemailer);
// export the mocked module
module.exports = nodemailerMock;
module.exports = nodemailermock;
```

@@ -153,5 +193,5 @@

```javascript
/**
/**
* Jest Test
* ./__tests__/my-test.js
* ./__tests__/my-test.js
**/

@@ -172,17 +212,43 @@ const { mock } = require('nodemailer');

Using typescript you can coerce the NodemailerMock type.
```typescript
/**
* Jest Test
* ./__tests__/my-test.js
**/
import { expect, test } from '@jest/globals';
// 'nodemailer' is automatically mocked in ./__mocks__/nodemailer.js
import * as nodemailer from 'nodemailer';
import { NodemailerMock } from 'nodemailer-mock';
const { mock } = nodemailer as unknown as NodemailerMock;
test('Send an email using the mocked nodemailer + typescript', async () => {
/* ... run your tests that send emails here */
// check the mock for our sent emails
const sentEmails = mock.getSentMail();
// there should be one
expect(sentEmails.length).toBe(1);
// and it should match the to address
expect(sentEmails[0].to).toBe('justin@to.com');
});
```
## example using mocha and mockery
Here is an example of using a mocked `nodemailer` class in a [`mocha`](https://www.npmjs.com/package/mocha) test using [`mockery`](https://www.npmjs.com/package/mockery). Make sure that any modules that `require()`'s a mocked module must be called AFTER the module is mocked or node will use the unmocked version from the module cache. Note that this example uses `async/await`. See the module tests for additional example code.
```javascript
/**
/**
* Mocha Test / Mockery Mock
* ./test/my-test.js
* ./test/my-test.js
**/
const should = require('should');
const { expect } = require('chai');
const mockery = require('mockery');
const nodemailerMock = require('nodemailer-mock');
const nodemailermock = require('nodemailer-mock');
describe('Tests that send email', async () {
/* This could be an app, Express, etc. It should be
/* This could be an app, Express, etc. It should be
instantiated *after* nodemailer is mocked. */

@@ -196,7 +262,7 @@ let app = null;

});
/* Once mocked, any code that calls require('nodemailer')
will get our nodemailerMock */
mockery.registerMock('nodemailer', nodemailerMock)
/* Once mocked, any code that calls require('nodemailer')
will get our nodemailermock */
mockery.registerMock('nodemailer', nodemailermock)
/*

@@ -207,13 +273,13 @@ ##################

*/
/* Make sure anything that uses nodemailer is loaded here,
/* Make sure anything that uses nodemailer is loaded here,
after it is mocked just above... */
const someModuleThatRequiresNodemailer = require('some-module-that-requires-nodemailer');
const moduleThatRequiresNodemailer = require('module-that-requires-nodemailer');
});
afterEach(async () {
// Reset the mock back to the defaults after each test
nodemailerMock.mock.reset();
nodemailermock.mock.reset();
});
after(async () {

@@ -224,45 +290,45 @@ // Remove our mocked nodemailer and disable mockery

});
it('should send an email using nodemailer-mock', async () {
// call a service that uses nodemailer
const response = ... // <-- your email code here
// a fake test for something on our response
response.value.should.be.exactly('value');
expect(response.value).to.equal('value');
// get the array of emails we sent
const sentMail = nodemailerMock.mock.getSentMail();
const sentMail = nodemailermock.mock.getSentMail();
// we should have sent one email
sentMail.length.should.be.exactly(1);
expect(sentMail.length).to.equal(1);
// check the email for something
sentMail[0].property.should.be.exactly('foobar');
expect(sentMail[0].property).to.equal('foobar');
});
it('should fail to send an email using nodemailer-mock', async () {
// tell the mock class to return an error
const err = new Error('My custom error');
nodemailerMock.mock.setShouldFailOnce();
nodemailerMock.mock.setFailResponse(err);
nodemailermock.mock.setShouldFailOnce();
nodemailermock.mock.setFailResponse(err);
// call a service that uses nodemailer
var response = ... // <-- your code here
// a fake test for something on our response
response.error.should.be.exactly(err);
expect(response.error).to.equal(err);
});
/* this will not work with jest as all nodemailers are mocked */
it('should verify using the real nodemailer transport', async () {
// tell the mock class to pass verify requests to nodemailer
nodemailerMock.mock.setMockedVerify(false);
nodemailermock.mock.setMockedVerify(false);
// call a service that uses nodemailer
var response = ... // <-- your code here
/* calls to transport.verify() will be passed through,
transport.sendMail() is still mocked */
/* calls to transport.verify() will be passed through,
transporter.send() is still mocked */
});
});
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc