测试数组是否不包含值

I'l*_*ack 4 javascript node.js jestjs

是否可以使用 Jest 来验证不userId: 123应该存在的。
例如:

[
  { UserId: 112, Name: "Tom" },
  { UserId: 314, Name: "Paul" },
  { UserId: 515, Name: "Bill" }
]
Run Code Online (Sandbox Code Playgroud)

如果没有带有UserId: 123.

Win*_*Win 10

正确的做法是检查数组是否与包含您要验证的对象的数组不相等。下面提供的示例向您展示了如何通过分层arrayContainingobjectContaining.

it("[PASS] - Check to see if the array does not contain John Smith", () => {
  expect([
    {
      user: 123,
      name: "Amelia Dawn"
    }
  ]).not.toEqual(
    expect.arrayContaining([
      expect.objectContaining({
        user: 5,
        name: "John Smith"
      })
    ])
  );
});

it("[FAILS] - Check to see if the array does not contain John Smith", () => {
  expect([
    {
      user: 5,
      name: "John Smith"
    },
    {
      user: 123,
      name: "Amelia Dawn"
    }
  ]).not.toEqual(
    expect.arrayContaining([
      expect.objectContaining({
        user: 5,
        name: "John Smith"
      })
    ])
  );
});
Run Code Online (Sandbox Code Playgroud)