遗憾的是,在 Chai 中做事的方法不止一种。
使用 to.be.an('undefined')
比使用 to.equal(undefined)
有什么好处吗?
我的直觉是重用/重新创建未定义会产生成本。我们的测试运行程序给出了各个测试的时间,似乎最重要的是先运行(在测试 watch 上,第二个运行更快,但进行两次单独的运行意味着它们都需要约 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.
作者似乎没有区分两者,甚至你的测试也表明它们或多或少是相等的。
我说,随心所欲。
编辑:
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)
污染可能会产生误报。
与之前的结论相同:选择有意义的东西。