在我的 E2E 测试中,我使用 mail-listener2检索电子邮件。它工作得很好,除了一个让我发疯并且无法解决的问题...我一直在搜索并发现有关此库/包的不同主题和问题,但只是无法真正找到解决方案。
以下:
我在多个 spec
文件(注册、登录、确认等)中使用该函数,这意味着在检索电子邮件时,我有时会收到错误的邮件。换句话说,该函数读取收件箱中的最后一封电子邮件,该电子邮件通常属于第一个测试。
有时,电子邮件到达收件箱的时间会晚于该功能正在读取的时间,因此它会读取错误的邮件。
正如我对 it()
函数确实有一个期望:
expect(email.subject).toEqual("电子邮件 1 的主题");
Expect(email['headers'].to).toEqual( userEmail );
因此测试中断,并出现以下错误:
- Expected 'user registration' to equal 'user confirmation'.
- Failed: Cannot read property '1' of null
- Expected '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="670d080f094903080227010808490302" rel="noreferrer noopener nofollow">[email protected]</a>' to equal '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a707b747f347e757f5a7c757534797577" rel="noreferrer noopener nofollow">[email protected]</a>'.
- Failed: Cannot read property '1' of null
有没有办法强制该函数只读取每个主题和每个用户的特定电子邮件?
最佳答案
是的,您可以在 node-imap 上找到此文档(由 mail-listener2
使用)。在该包内的 search
上搜索段落/项目符号,以下代码片段可帮助您找到它:
For criteria types that require arguments, use an array instead of just the string criteria type name (e.g. ['FROM', '[email protected]']).
在下面,他们列出了您可以使用的其他几个搜索条件,其中有to/from
作为您的用户条件,以及subject
作为您的用户条件。因此,将其应用于 mail-listener2
,您可以在 searchFilter
属性中使用它:
mailListener = new MailListener({
...(other options),
searchFilter: [['FROM', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9dfce8e9f2f0fce9f8f9ddf0f8eeeefcfaf8b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a>'], ['SUBJECT', 'subject for e-mail 1']],
});
如果您需要针对不同的测试使用不同的搜索条件,您可以使用新的 searchFilter
条件为每个测试启动一个新的 mail-listener
session 。
关于node.js - mail-listener2 - 如何防止功能读取错误的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49177639/