@codeceptjs/mailslurp-helper

中文文档
1.5.2发布于 2025/10/20ISCTypeScriptCommonJS近 30 天下载 1.9万

MailSlurp helper for testing emails https://mailslurp.com

原始使用文档
文档目录27 节
  1. Setup
  2. Use Cases
  3. Use Case: Restore Password
  4. Switching Between Mailboxes
  5. Using Custom Assertions In Tests
  6. LICENSE
  7. API
  8. Configuration
  9. Configuration
  10. haveNewMailbox
  11. haveExistingMailbox
  12. openMailbox
  13. sendEmail
  14. waitForLatestEmail
  15. waitForEmailMatching
  16. waitForNthEmail
  17. grabEmailsMatching
  18. grabAllEmailsFromMailbox
  19. seeInEmailSubject
  20. dontSeeInEmailSubject
  21. seeInEmailBody
  22. dontSeeInEmailBody
  23. seeEmailIsFrom
  24. seeEmailSubjectEquals
  25. dontSeeEmailSubjectEquals
  26. seeNumberOfEmailAttachments
  27. seeEmailAttachment

MailSlurp helper for CodeceptJS

MailSlurp helper for testing emails with MailSlurp service. MailSlurp creates disposable mailbox for each test and removes it after a test.

Setup

Register and obtain API key from MailSlurp.

npm i @codeceptjs/mailslurp-helper --save

Enable helper in codecept.conf.js.

helpers: {
  MailSlurp: {
    apiKey: '<insert api key here>',
    require: '@codeceptjs/mailslurp-helper'    
  },
}

Use Cases

Use this helper in your tests to check email interactions. The most popular one

  • Account activation
  • Restore forgotten password
  • Action verification

Use Case: Restore Password

const mailbox = await I.haveNewMailbox();
// wait 10 seconds for an email
const email = await I.waitForLatestEmail(10);
I.seeInEmailSubject('Restore Password');
I.seeInEmailBody('Click link to restore password');
const restoreUrl = email.body.match(/http(s):\/\/(.*?)\s/)[0];
I.amOnPage(restoreUrl);

Switching Between Mailboxes

const mailbox1 = await I.haveNewMailbox();
const mailbox2 = await I.haveNewMailbox();
I.openMailbox(mailbox1);
const email = I.waitForEmailMatching({ subject: 'Register' });

Using Custom Assertions In Tests

const assert = require('assert');
const mailbox = await I.haveNewMailbox();
const email = await I.waitForEmailMatching({ subject: 'Thanks' });
assert.eql(email.subject, 'Thanks for registering')

LICENSE

MIT

API

Table of Contents

Configuration

Allows to use real emails in E2E tests via MailSlurp service. Sign up for an account at MailSlurp to start.

A helper requires apiKey from MailSlurp to start

helpers: {
  MailSlurp: {
    apiKey: '<insert api key here>',
    require: '@codeceptjs/mailslurp-helper'
  },
}

Use .env file and environment variables to store sensitive data like API keys

Configuration

  • apiKey (required) - api key from MailSlurp
  • timeout (default: 10000) - time to wait for emails in milliseconds.
  • debug (default: false) - print debug logs

Type: {apiKey: string, timeout: number?, debug: boolean?}

Properties

haveNewMailbox

Creates a new mailbox. A mailbox will be deleted after a test. Switches to last created mailbox.

const mailbox = await I.haveNewMailbox();

haveExistingMailbox

Use an existing mailbox.

const mailbox = await I.haveExistingMailbox('94cxxxf4-7231-46ce-9f40-xxxcae39xxxx');

Parameters

  • mailboxId string ID of an existing MailSlurp inbox.

Returns Promise<Inbox>

openMailbox

Change a current mailbox to a provided one:

const mailbox1 = await I.haveMailbox();
const mailbox2 = await I.haveMailbox();
// mailbox2 is now default mailbox
// switch back to mailbox1
I.openMailbox(mailbox)

Parameters

  • mailbox

sendEmail

Sends an email from current mailbox, created by I.haveNewMailbox().

I.sendEmail({
  to: ['[email protected]'],
  subject: 'Hello',
  body: 'World'
});

Parameters

  • data

waitForLatestEmail

Waits for the first email in mailbox. If mailbox is not empty - opens the last email.

I.waitForLatestEmail()
// wait for 30 seconds for an email
I.waitForLatestEmail(30);

Parameters

  • sec num? Number of seconds to wait.

Returns Promise<Email> an email received.

waitForEmailMatching

Wait for an exact email matched by query. You can match emails by from, to, subject, cc, bcc fields. My default, non-strcit matching enabled, so it searches for inclusion of a string. For a strict matching (equality) prepend a value with = prefix.

 // wait for email with 'password' in subject
const email = await I.waitForEmailMatching({
 subject: 'password',
});

// wait 30 seconds for email with exact subject
const email = await I.waitForEmailMatching({
 subject: '=Forgot password',
}, 30);

//
const email = await I.waitForEmailMatching({
 from: '@mysite.com', // find anything from mysite
 subject: 'Restore password', // with Restore password in subject
});

Parameters

  • query object to locate an email
  • sec num? Number of seconds to wait.

Returns Promise<Email> an email received.

waitForNthEmail

Wait for exact number of emails in mailbox. Returns the last email in the list.

// wait for 2 emails
I.waitForNthEmail(2);
// wait for 5 emails for 60 seconds
I.waitForNthEmail(5, 60);
// wait for 2 emails and return the last one
const email = await I.waitForNthEmail(2);

Parameters

  • number
  • sec

grabEmailsMatching

Returns a bunch of emails matched by query. Similar to waitForEmailMatching but returns an array of emails.

// return 2 emails from '[email protected]'
const emails = await I.grabEmailsMatching({ from: '[email protected]'}, 2);

Parameters

  • query object to locate an email
  • num num? Number of emails to return.

Returns Promise<[Email]> emails matching criteria.

grabAllEmailsFromMailbox

Returns all emails from a mailbox.

const emails = await I.grabAllEmailsFromMailbox();

Returns Promise<[Email]> emails.

seeInEmailSubject

Checks that current email subject contains a text.

I.seeInEmailSubject('Restore password');

Requires an opened email. Use either waitForEmail* methods to open. Or open manually with I.openEmail() method.

Parameters

  • text

dontSeeInEmailSubject

Checks that current email subject does not contain a text.

I.seeInEmailSubject('Restore password');

Requires an opened email. Use either waitForEmail* methods to open. Or open manually with I.openEmail() method.

Parameters

  • text

seeInEmailBody

Checks that current email body contains a text.

I.seeInEmailBody('Click link');

Requires an opened email. Use either waitForEmail* methods to open. Or open manually with I.openEmail() method.

Parameters

  • text

dontSeeInEmailBody

Checks that current email body does not contain a text.

I.dontSeeInEmailBody('Click link');

Requires an opened email. Use either waitForEmail* methods to open. Or open manually with I.openEmail() method.

Parameters

  • text

seeEmailIsFrom

Checks that email is from a specified address.

I.seeEmailIsFrom('[email protected]');

Requires an opened email. Use either waitForEmail* methods to open. Or open manually with I.openEmail() method.

Parameters

  • text

seeEmailSubjectEquals

Checks that current email subject equals to text.

I.seeEmailSubjectEquals('Restore password');

Requires an opened email. Use either waitForEmail* methods to open. Or open manually with I.openEmail() method.

Parameters

  • text

dontSeeEmailSubjectEquals

Checks that current email subject doesn't equal to text.

I.dontSeeEmailSubjectEquals('Restore password');

Requires an opened email. Use either waitForEmail* methods to open. Or open manually with I.openEmail() method.

Parameters

  • text

seeNumberOfEmailAttachments

Checks that current email has expected number of attachments.

I.seeNumberOfEmailAttachments(2);

Requires an opened email. Use either waitForEmail* methods to open. Or open manually with I.openEmail() method.

Parameters

  • number

seeEmailAttachment

Checks that current email has an attachment with specified name.

I.seeEmailAttachment('ExampleAttachment.pdf');

Be aware that Mailslurp SDK removes special characters in name of attachment, e.g. "Example-Attachment.pdf" will have name "ExampleAttachment.pdf".

Requires an opened email. Use either waitForEmail* methods to open. Or open manually with I.openEmail() method.

Parameters

  • nameRegExp

版本与发布记录

@codeceptjs/mailslurp-helper 共发布 25 个版本,首次发布于 2019/09/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.2latest2025/10/201.1 年前
1.5.12025/10/011.1 年前
1.5.02025/09/301.1 年前
1.4.42025/09/301.1 年前
1.4.32025/03/201.6 年前
1.4.22025/02/111.7 年前

关于 @codeceptjs/mailslurp-helper 的常见问题

@codeceptjs/mailslurp-helper 是什么?

MailSlurp helper for testing emails https://mailslurp.com

如何安装 @codeceptjs/mailslurp-helper?

在项目目录运行 npm install @codeceptjs/mailslurp-helper,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:codeceptjs(>=3.6.5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@codeceptjs/mailslurp-helper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codeceptjs/mailslurp-helper 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@codeceptjs/mailslurp-helper 可以商用吗?

@codeceptjs/mailslurp-helper 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codeceptjs/mailslurp-helper 的下载量有多少?

根据 npm 官方下载统计,@codeceptjs/mailslurp-helper 在 2026/10/10 – 2026/11/08 这 30 天内被下载 18,941 次,日均约 631 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codeceptjs/mailslurp-helper 还在维护吗?

最新版本 1.5.2 发布于 2025/10/20(1.1 年前);至今共发布 25 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/09/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。