fluid-mail-test

A Fluid component that provides an SMTP server and client that can be used for testing purposes.

Usage no npm install needed!

<script type="module">
  import fluidMailTest from 'https://cdn.skypack.dev/fluid-mail-test';
</script>

README

Introduction

This package provides a test mail server based on simplesmtp, and a mail client based on nodemailer-smtp-transport.

Global Functions

fluid.mail.test.loadTestingSupport()

  • Returns: Nothing.

Require the files that define our case holder and environment (see below). Allows third-parties to more easily reuse our test fixtures.

Components

fluid.test.mail.smtp

A test mail server that allows you to examine the contents of outgoing mail messages from within Fluid IoC tests. This allows you to do things like:

  1. Confirm that a link send to a user via email is actually usable.
  2. Confirm that mail message content is rendered correctly.

When a message is received, it is saved to the cache file, and then an onMessageReceived event is fired. In your tests you will typically:

  1. Send mail.
  2. Listen for the onMessageReceived event.
  3. Examine the contents of {fluid.test.mail.smtp}.currentMessageFile.

Note that the filename for the cache file is generated using a timestamp, and is located in os.tmpdir() by default.

Component Options

Option Type Description
port {Number} The port number to listen on. Defaults to 4025.
simpleSmtp {Object} The configuration options to pass on to simplesmtp.createServer. See the simplesmtp documentation for details.

fluid.test.mail.mailer

A wrapper around nodemailer-smtp-transport that can be used to send test messages to an arbitrary SMTP server.

Component Options

Option Type Description
transportOptions {Object} Configuration options to pass on to nodemailer-smtp-transport. See their documentation for details.
smtpPort {Number} The port on transportOptions.host we will attempt to connect to.

Component Invokers

{that}.sendMessage(mailOptions)
  • mailOptions: {Object} The mail options to pass on to nodemailer-smtp-transport. The full syntax available can be found in the nodemailer documentation. See below for an example.
  • Returns: Nothing.

Send a message using nodemailer-smtp-transport. Fires onSuccess if the message is sent succesfully, or onError if the message fails. Here is an example of typical mailOptions:

{
  from:    "sender@site1.com",
  to:      "recipient@site2.com",
  cc:      "overseer@site3.com",
  subject: "Sample subject...",
  text:    "Text body of the message.\n",
  html:    "<p>HTML body of the message.</p>\n"
}

Note that the to and cc elements can also be passed an array of email addresses.

fluid.test.mail.environment

A fluid.test.testEnvironment that constructs an instance of fluid.test.mail.smtp when its constructServer event is fired. Intended for use with something like the fluid.test.mail.caseholder caseholder (see below).

Component Options

Option Type Description
port {Number} The port the SMTP server should listen on.

fluid.test.mail.caseholder

An instance of fluid.express.tests.caseHolder.base, which is a case holder that allows wiring in default "first steps" and "last steps" for each test. This grade includes the "startup" sequences for fluid.test.mail.environment, which launch the mail server, and then wait for it to finish starting up before continuing. If you need different startup steps, extend fluid.express.tests.caseHolder.base instead.

The only practical difference between this and a stock caseholder is that your tests should be stored in options.rawModules rather than options.modules.