我们应该更喜欢 Chai 的 `to.be.an('undefined')` 还是 `to.equal(undefined)`?

Should we prefer Chai's `to.be.an('undefined')` vs `to.equal(undefined)`?

遗憾的是,在 Chai 中做事的方式不止一种。

使用 to.be.an('undefined') 而不是 to.equal(undefined) 有任何好处吗?

我的直觉是 reusing/recreating 未定义的成本。我们的测试运行器给出了单独测试的时间,看起来哪个先运行更重要(在测试手表上,第二个运行速度更快,但进行两次单独运行意味着它们都需要大约 2 秒(完整设置))。

我认为这并不重要。我找到的最接近答案的是 this article

In that sense, code the expectation in a human-like language, declarative BDD style using expect or should and not using custom code.

作者似乎没有在两者之间做出区分,甚至您的测试也说它们或多或少是相等的。

我说什么都行。

编辑:

基于this stack overflow answer

The more deep is a property nested, more time will be required to perform the property lookup.

这意味着 to.be.an('undefined') 实际上会比 to.equal(undefined) 慢,因为额外的查找,但 IMO 随之而来的 prototype 污染可能会产生误报。

真的和以前一样的结论:选择有意义的。