如何在Jest中验证对象是否为数组且不为空

gkl3eglg  于 2023-05-15  发布在  Jest
关注(0)|答案(3)|浏览(370)

我正在尝试创建以下对象测试:
1.验证对象是否为数组
1.验证此数组是否不为空
在Chai,这将是:

expect(response['output']['data']['results']).to.be.an('array').not.empty;

我在《Jest》中写道:

expect(Array.isArray(response['output']['data']['results'])).toBe(true);
expect(response['output']['data']['results'].length > 0).toBe(true);

在Jest中是否有更清晰的方法?

kpbpu008

kpbpu0081#

我建议你这样测试

it('should have nested property output.data.results, which is an array containing anything', () => {
  expect(response).toHaveProperty('output.data.results', expect.arrayContaining([
    expect.anything()
  ]))    
})

优点是:

  • 这是一个单一的Assert(否则,如果第一个Assert失败,第二个Assert的错误将不会显示)
  • 整个result被传递给expect()函数,因此如果某些嵌套属性丢失-整个对象将显示在错误中

工作示例
在这里,您可以看到如何表示故障的不同示例。

4xrmg8kj

4xrmg8kj2#

可以从jest-extended使用.toBeArrayOfSize().toSatisfy(predicate)
jest-extended旨在为Jest的默认匹配器添加额外的匹配器,使测试一切变得容易
示例:

describe('76233061', () => {
  test('should pass', () => {
    const response = {
      output: {
        data: {
          results: [1]
        }
      }
    }
    expect(response.output.data.results).toSatisfy((x) => Array.isArray(x) && x.length > 0);
    expect(response.output.data.results).toBeArrayOfSize(1);
  })
})
w1e3prcc

w1e3prcc3#

我正在尝试创建以下对象测试:
1.验证对象是否为数组
1.验证此数组是否不为空
所以(只有Jest's built-in expectations):

expect(thing).toBeInstanceOf(Array);
          // ^~~~~~~~~~~~~~~~~~~~~~ 1. is an array
expect(thing).not.toHaveLength(0);
          // ^~~~~~~~~~~~~~~~~~~~ 2. is not empty

运行一些示例:

[
    "foo",
    {},
    [],
    ["foo"],
    ["foo", "bar", "baz"],
].forEach((thing) => {
    it(`tests ${JSON.stringify(thing)}`, () => {
        expect(thing).toBeInstanceOf(Array);
        expect(thing).not.toHaveLength(0);
    });
});

给出预期结果:

FAIL  src/non-empty-array.spec.ts
  ✕ tests "foo" (1 ms)
  ✕ tests {}
  ✕ tests [] (1 ms)
  ✓ tests ["foo"] (1 ms)
  ✓ tests ["foo","bar","baz"]

有用的失败:

  • 告诉你它是否原始:
● tests "foo"

    expect(received).toBeInstanceOf(expected)

    Expected constructor: Array

    Received value has no prototype
    Received value: "foo"
  • 告诉你它是否是其他事物的示例:
● tests {}

    expect(received).toBeInstanceOf(expected)

    Expected constructor: Array
    Received constructor: Object
  • 告诉你它是否是空的:
● tests []

    expect(received).not.toHaveLength(expected)

    Expected length: not 0
    Received array:      []

当你把所有的期望值都拉平到expect(someBool).toBe(true)时,你就失去了任何有用的诊断信息;* 每一个 * 这些失败的情况下,与您的原始尝试给出:

expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: false

并且它在哪个特定行上失败是输出的唯一有用部分。
测试Lin Du's answers

  • .toBeArrayOfSize要求您指定确切的长度。我也尝试了expect(thing).toBeArrayOfSize(expect.toBePositive()),但不对称匹配器没有正确应用:
expect(received).toBeArrayOfSize(expected)

    Expected value to be an array of size:
      toBePositive<>
    Received:
      value: ["foo"]
      length: 1

.not.toBeArrayOfSize(0),但它不会在字符串和对象的情况下失败,只会在[]的情况下失败。

  • .toSatisfy确实显示了什么值没有满足期望,但仅此而已:
expect(received).toSatisfy()

    Expected value to satisfy:
      [Function anonymous]
    Received:
      []

使用以下工具测试Teneff's answer

expect({ thing }).toHaveProperty("thing", expect.arrayContaining([expect.anything()]));

你只会看到不匹配的属性值,例如:

expect(received).toHaveProperty(path, value)

    Expected path: "thing"

    Expected value: ArrayContaining [Anything]
    Received value: []

但如果它不能跟随整个路径,它会根据需要显示对象的更多上下文:

expect(received).toHaveProperty(path, value)

    Expected path: "foo.bar"
    Received path: []

    Expected value: ArrayContaining [Anything]
    Received value: {"thing": []}

对于像你这样的情况,数组在一个嵌套的对象中,这是非常有用的。
我不认为 “这是一个单一的Assert(否则如果第一个Assert失败,第二个Assert的错误将不会显示)" 在这里特别有用-如果它 * 不是 * 一个数组,那么继续Assert它的长度属性没有多大意义。
我也试着看看如果你要求jest-codemods将你从Chai BDD迁移到Jest,它会推荐什么,但这没有多大帮助:

expect(thing).toEqual([])

为了完整起见,请注意,如果你真的想,你可以 * 使用Chai*。如果抛出错误,Jest将无法通过测试;它不在乎是由实现、它自己的期望还是柴的期望。也就是说,关于失败的信息可能有点令人困惑:

Expected value not be strictly equal to:
      undefined
    Received:
      []
    
    Message:
      expected [] not to be empty

    Difference:

      Comparing two different types of values. Expected undefined but received array.

这是由于在Chai错误对象上设置的属性-提供了actualmessage,所以Jest显示了它们,但(因为它不是直接比较)expected没有。这意味着它不能显示有用的差异。

相关问题