使用 Cypress 和 Node-imap 测试接收邮件失败,缺少 'net' 模块

Testing to receive mail with Cypress and Node-imap fails with missing 'net' module

我想使用 node-imap 或来自 Cypress 中 NPM 的 imap 包来检查是否收到某些电子邮件并检查它们的内容。

当我尝试从 imap github 和文档中获取的示例中的基本副本时,它失败并出现以下错误:

./node_modules/imap/lib/Connection.js
Module not found: Error: Can't resolve 'net' in '/Users/username/Documents/test-e2e/node_modules/imap/lib'
resolve 'net' in '/Users/username/Documents/test-e2e/node_modules/imap/lib'
  Parsed request is a module
  using description file: /Users/username/Documents/test-e2e/node_modules/imap/package.json (relative path: ./lib)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      /Users/username/Documents/test-e2e/node_modules/imap/lib/node_modules doesn't exist or is not a directory
      /Users/username/Documents/test-e2e/node_modules/node_modules doesn't exist or is not a directory
      /Users/username/Documents/node_modules doesn't exist or is not a directory
      /Users/username/node_modules doesn't exist or is not a directory
      /Users/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
      looking for modules in /Users/username/Documents/test-e2e/node_modules/imap/node_modules
        using description file: /Users/username/Documents/test-e2e/node_modules/imap/package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
      looking for modules in /Users/username/Documents/test-e2e/node_modules
        using description file: /Users/username/Documents/test-e2e/package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: /Users/username/Documents/test-e2e/node_modules/imap/package.json (relative path: ./node_modules/net)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
          using description file: /Users/username/Documents/test-e2e/package.json (relative path: ./node_modules/net)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/username/Documents/test-e2e/node_modules/imap/node_modules/net doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/username/Documents/test-e2e/node_modules/net doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/username/Documents/test-e2e/node_modules/imap/node_modules/net.ts doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/username/Documents/test-e2e/node_modules/net.ts doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/username/Documents/test-e2e/node_modules/imap/node_modules/net.js doesn't exist
              /Users/username/Documents/test-e2e/node_modules/net.js doesn't exist
            as directory
              /Users/username/Documents/test-e2e/node_modules/imap/node_modules/net doesn't exist
            as directory
              /Users/username/Documents/test-e2e/node_modules/net doesn't exist

因此无法进行测试 运行。 我试着寻找这个 'net',也与 imap 包结合使用,但我真的找不到任何解决方案。 甚至可以将 Imap 与 Cypress 一起使用吗?

Cypress 在浏览器中执行它的代码。所以你不能使用这样的 NPM 模块。 要解决此问题,您必须使用 cy.task。赛普拉斯在节点进程中执行任务,因此您可以使用任何您想要的库。任务必须在插件文件中定义,因为该文件是在节点上下文中加载的。

因此您必须在 cypress.json 中告诉 cypress 您正在使用插件文件:

{
    ...
    "pluginsFile": "cypress/plugins/plugins.js",
    ...
  }

然后在plugins.js中定义一个任务:

on('task', {
    sendMail({ subject, to, body }) {
      return getEmail(subject, to, body);
    }
  });

getEmail 应该是您的示例代码。

像这样使用任务:

cy.task("sendMail", { subject, to, body }, { timeout: 30000 });