Code Monkey home page Code Monkey logo

codeceptjs-testmailapp-helper's Introduction

Testmail.app helper for CodeceptJS

CodeceptJs helper for end-to-end testing using the Testmail.app service. Testmail service does not create disposable inboxes as other services do, but their namespace/tag support allows this helper to simulate inbox creation.

Setup

First, create an account with Testmail.app and obtain your API key and the namespace that you will use for testing. Then, install the helper using the following command:

npm i codeceptjs-testmailapp-helper --save

Add the helper to your codecept.conf.js file:

helpers: {
  TestmailHelper: {
    apiKey: '<testmail.app API key>',
    namespace: '<testmail.app namespace>',
    require: 'codeceptjs-testmailapp-helper'
  },
}

The simplest use case

The following code will allow your test to create a new random inbox and then receive an email:

const inbox = await I.haveInbox();

// Trigger some code that will send an email
await I.needNotification(inbox.email);

// Wait for the email to be received.
const email = await I.receiveEmail();

// check that sender is the expected one.
assert(email.from === "Notifications <[email protected]>");

Functions

The helper exposes only three functions you should care about. They are listed below.

haveInbox()

Creates a new inbox. The email address is generated using the account namespace, a random tag and the standard testmail.app domain. The function returns an inbox object that you can use later in the other functions.

Example:

const inbox = await I.haveInbox();

// Use the inbox.email to obtain the generated random email.
I.say(`The new email is ${inbox.email}.`);

haveInbox(email)

Re-creates an inbox from a given email address. The email address is expected to be using the same account namespace, a tag and the standard testmail.app domain. The function return an inbox object that you can use later in the other functions.

This function is useful to when receiving emails for previously created accounts.

Example:

const inbox = await I.haveInbox("[email protected]");

// Use the inbox.email to obtain the same email.
I.say(`The new email is ${inbox.email}.`);

receiveEmails()

Waits and returns all new emails since the last call to receiveEmails(). Note that calling this function without an inbox argument will assume the emails are loaded for the inbox that was last created using haveInbox() function.

If no emails are retrieved in the given timeout period, an error is raised.

Example:

const emails = await I.receiveEmails();

// use the properties from the email.
assert(emails[0].from === "me");

receiveEmails(inbox, [timeout])

Waits for all new emails since last call to receiveEmails() for a given inbox. The inbox has to be created beforehand using the haveInbox() function. You can supply a timeout (in seconds) as the last argument to this function.

If no emails are retrieved in the given timeout period, an error is raised.

Example:

const inbox = await I.haveInbox();
// tests ...
const emails = await I.receiveEmails(inbox, 60); // only wait 60 seconds.

// use the properties from the email.
assert(emails[0].from === "me");

receiveEmail() / receiveEmail(inbox, [timeout])

And, because waiting for multiple emails is not very useful, there is a version of the function that only returns the latest received email.

If no emails are retrieved in the given timeout period, an error is raised.

Example:

const email = await I.receiveEmail();

// use the properties from the email.
assert(email[0].from === "me");

The email object

The email object consists from the following properties:

{
    from,
    subject,
    html,
    text,
    attachments {
      filename
      contentType
      downloadUrl
    }
}

codeceptjs-testmailapp-helper's People

Contributors

kobenguyent avatar pavkam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kobenguyent

codeceptjs-testmailapp-helper's Issues

Could the Email object be extended to return also attachments information

What am I trying to achieve?
I want to check if a mail is received on a certain address using Testmail.app and if the received email contains the expected attachments.

Issue
Currently, helper does not support attachments in the Email object

Suggestion
Attachments property to be added to the query used to retrieve the last mail in receiveEmails() function.
That way if the user needs attachments to be checked this would be possible since they will be part of the response data.

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.