当前位置: 首页 > 知识库问答 >
问题:

如何在Node.js服务器中用Jest解决UnhandledPromiseRejectionWarning

花阳辉
2023-03-14

我的测试正在正确通过,但我一直收到一个未处理的PromiseerEjectionWarning。 有什么办法可以解决这个问题吗? 我试过许多办法,但似乎没有一个管用。

node:32535) UnhandledPromiseRejectionWarning: Error: expect(received).toMatchObject(expected)

Matcher error: received value must be a non-null object

Received has value: undefined (node:32535) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:32535) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我的测试文件如下:

jest.mock("./http");
const { fetchAllUsersData } = require("./http");

test("Data returned from server should be a list of users ", async () => {
  fetchAllUsersData()
    .catch((errorMessage) => expect(errorMessage).toThrowError(Error))
    .then((usersRetrieved) => {
      let users = usersRetrieved[0];
      let userModel = {
        id: 1,
        first_name: "Peggy",
        last_name: "Poppins",
        email: "mpoppins0@squidoo.com",
        ip_address: "192.54.212.191",
        latitude: 34.003135,
        longitude: -117.7228641222,
      };

      expect(users).toMatchObject(userModel);
    });
});

这是我在模拟文件夹中的文件:

      const fetchAllUsersData = () => {
        return Promise.resolve({
          data: [
            {
              id: 1,
              first_name: "Merry",
              last_name: "Poppins",
              email: "mpoppins0@squidoo.com",
              ip_address: "192.54.212.191",
              latitude: 34.003135,
              longitude: -117.7228641,
            },
            {
              id: 15,
              first_name: "George",
              last_name: "Foreman",
              email: "gforeman@clear.com",
              ip_address: "12.564.124.521",
              latitude: 23.592254,
              longitude: 125.454227,
            },
          ],
        });
      };

      exports.fetchAllUsersData = fetchAllUsersData;

共有1个答案

乐正秦斩
2023-03-14

出现此错误的原因是没有将一个承诺链接到从测试中返回。 浮动承诺是一种反模式。 async.。await允许以比原始承诺更少的规则来实现这一点。 将原始承诺与async一起使用通常没有意义。 另外,在同一个测试中同时测试成功和失败的请求是没有意义的,预期的响应应该是预先确定的。

只有当errorMessage是同步抛出错误的函数时,Expect(errorMessage).ToThrowError(Error)才会工作。 如果FetChallUsersData拒绝ErrorMessage对象,则无法按预期工作。

它很可能应该在一个测试中:

test("...", async () => {
  const usersRetrieved = await fetchAllUsersData();
  ...
  expect(users).toMatchObject(userModel);
});

在另一个测试中:

test("...", async () => {
  await expect(fetchAllUsersData()).rejects.toThrowError(Error);
});

另外,如果您测试__mock__中提供的您自己的测试代码,那么测试就没有很好的用途。 他们应该测试生产代码。

 类似资料:
  • 问题内容: 给定一个SSL密钥和证书,如何创建HTTPS服务? 问题答案: 我发现以下示例。 这适用于节点v0.1.94-v0.3.1。在新版本的节点中被删除。 直接从该来源:

  • 问题内容: 收到以下错误: TypeError:Object.values不是函数。 从这个对计算器的问题-我看是不是在所有的浏览器都支持。 但是我在服务器端的Node.js中使用该功能-如何在Node.js中使用它看起来如此直观? 问题答案: 是ES2017中的新功能。这是非常出血的边缘。从7.0版开始,Node.js完全支持它。 6.8.1支持它,但是它被认为是不稳定的并且被锁定在标志后面。

  • 问题内容: 我已经安装并正在osx上运行一个node.js服务器。我已经释放了一个聊天模块,并且正在愉快地运行它。我更改了一些内容,需要重新启动服务器才能看到效果。 我只知道如何通过关闭终端窗口,然后重新对其进行操作,然后再次运行节点chatdemo.js来重新启动。 有什么方法可以重启而不关闭终端吗? 谢谢。 问题答案: 如果它仅在运行(不是守护程序),则使用。 如果是守护进程,则可以尝试: 凡

  • 异常跟踪: “”java.lang.IllegalStateException:在org.apache.catalina.core.applicationDispatcher.doForward(applicationDispatcher.java:328)在org.apache.catalina.core.applicationDispatcher.doForward(applicationDi

  • 您知道如何强制远程web服务器关闭连接吗? 我的简化代码:

  • 我在运行节点服务器时一直面临以下错误,但代码以前可以正常工作。有专家可以解决这个问题吗? 下面是server.js的错误部分

  • 问题内容: 我实际上正在对ExpressJS服务器进行一些负载测试,并且我注意到服务器发送的响应包括一个“ Connection:Keep- Alive”标头。据我了解,该连接将保持打开状态,直到服务器或客户端发送“ Connection:Close”标头为止。 在某些实现中,“ Connection:Keep-Alive”标头带有“ Keep-Alive”标头,用于设置连接超时和通过该连接发送的