如何在 Jest 中像 Chai 一样简单地创建测试:“验证对象是一个数组而不是空的。”

问题描述 投票:0回答:0

我是第一次使用 Jest,并且已经使用 Mocha+Chai+Sinon 8 年多了。所以,我承认我会有偏见。

与 Chai 相比,我对 Jest 的第一印象

  • Chai 是一个比 Jest 更干净、更健壮的测试框架。
  • Chai 更好,更容易获得在线文档和测试粒度。

例如,令人惊讶的是,在 Jest 中创建以下对象测试是多么困难:

  1. 验证对象是否为数组
  2. 如果这个数组不为空则验证

这是我写的:

开玩笑

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

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

请求

  1. 是否有比 Jest 更轻松、更简洁的方法?
  2. 最好的 Jest 文档,带有测试和模拟示例,可与 Chai 和 Sinon 轻松获得的内容相媲美?

谢谢

typescript jestjs mocking chai sinon
© www.soinside.com 2019 - 2024. All rights reserved.