简体   繁体   English

Node.js断言库与其他断言库

[英]Node.js assert library vs. other assert libraries

According to node.js assert library documentation : 根据node.js断言库文档

The module is intended for internal use by Node.js, but can be used in application code via require('assert'). 该模块供Node.js内部使用,但可以通过require('assert')在应用程序代码中使用。 However, assert is not a testing framework, and is not intended to be used as a general purpose assertion library. 但是,assert不是测试框架,并不打算用作通用断言库。

I was looking at Chai as an alternative assert library (no BDD API, only the assert API), and at the end I see that the assert functionality is very similar. 我在看Chai作为替代断言库(没有BDD API,只有断言API),最后我看到断言功能非常相似。

Why Chai's assert library is a better assert library? 为什么Chai的断言库是一个更好的断言库? It does everything than node.js does (beside being just more rich in terms of assertion available, but that's just syntactic sugar-coating). 它完成了除node.js之外的所有工作(除了在可用的断言方面更加丰富,但这只是语法糖涂层)。 Even simple things like the total count of assert executed is not available on both. 即使是简单的事情,例如执行的assert总数也不可用。

Am I missing something ? 我错过了什么吗?

UPDATE (April 2017): Node.js no longer warns people away from using assert so the answer below is now outdated. 更新(2017年4月):Node.js不再警告人们不要使用assert所以下面的答案现在已经过时了。 Leaving it for historical interest, though. 但是,留下它的历史兴趣。

Here's the answer I posted to a very similar question on the Node.js issue tracker . 这是我在Node.js问题跟踪器上发布的一个非常相似的问题的答案

https://github.com/nodejs/node/issues/4532 and other issues allude to the reason the documentation recommends against using assert for unit testing: There are edge case bugs (or at least certainly surprises) and missing features. https://github.com/nodejs/node/issues/4532和其他问题暗示了文档建议不要使用assert进行单元测试的原因:有边缘案例错误(或至少肯定是惊喜)和缺少的功能。

A little more context: Knowing what we now know, if we were designing/building Node.js core all over again, the assert module would either not exist in Node.js or else consist of far fewer functions--quite possibly just assert() (which is currently an alias for assert.ok() ). 更多上下文:了解我们现在所知道的,如果我们再次设计/构建Node.js核心,那么assert模块要么不存在于Node.js中,要么由更少的函数组成 - 很可能只是assert() (目前是assert.ok()的别名)。

The reasons for this, at least from my perspective, are: 至少从我的角度来看,其原因是:

  • all the stuff being done in assert could easily be done in userland assert中完成的所有操作都可以在userland中轻松完成
  • core efforts are better spent elsewhere than perfecting a unit testing module that can be done in userland 除了完善可在用户区完成的单元测试模块之外,核心工作更好地用于其他地方

There's additional context that others may choose to add here or not (such as why, all things being equal, we would favor keeping core small and doing things in userland). 还有其他人可能选择在这里添加的附加上下文(例如为什么,在所有条件相同的情况下,我们倾向于保持核心小而在用户区域中做事)。 But that's the so-called 30,000 foot view. 但这就是所谓的30,000英尺的视野。

Since assert has been in Node.js for a long time and a lot of the ecosystem depends on it, we are unlikely (at least as best as I can tell at the current time) to ever remove assert.throws() and friends. 由于assert已经在Node.js中存在了很长时间并且很多生态系统依赖于它,因此我们不太可能(至少在当前时间我能说的最好)去掉assert.throws()和朋友。 It would break too much stuff. 它会打破太多东西。 But we can discourage people from using assert and encourage them to use userland modules that are maintained by people who care deeply about them and who aggressively fix edge-case bugs and who add cool new features when it makes sense. 但我们可以阻止人们使用assert并鼓励他们使用由深切关注他们的人维护的用户态模块,并积极修复边缘案例错误,并在有意义的时候添加很酷的新功能。 So that's what that's all about. 这就是全部。

True, if you're doing straightforward assertions with simple cases, assert probably will meet your needs. 没错,如果您使用简单的案例进行简单的断言, assert可能会满足您的需求。 But if you ever outgrow assert , you'll be better off with chai or whatever. 但是,如果你不断发表assert ,你将会更好地与chai或其他什么。 So we encourage people to start there. 所以我们鼓励人们从那里开始。 It's better for them (usually) and better for us (usually). 对他们(通常)更好,对我们(通常)更好。

I hope this is helpful and answers your question. 我希望这有用并回答你的问题。

I guess since nobody gave me any good feedback I'll try to provide some light on my original question after some time of working with both node.js assert and chai's assert. 我想因为没有人给我任何好的反馈,我会尝试在使用node.js assert和chai的断言一段时间之后对我原来的问题提供一些启示。

The answer at the very end is that functionality-wise they are the same. 最后的答案是功能方面它们是相同的。 The only reason why chai's assert exist is so if you read the code you can get a better understanding of the tests, but that's about it. chai的断言存在的唯一原因是,如果你阅读代码,你可以更好地理解测试,但就是这样。

For example, testing for a null value with Node.js: 例如,使用Node.js测试空值:

assert(foo === null); 断言(foo === null);

And using chai: 并使用柴:

assert.isNull(foo); assert.isNull(FOO);

They are perfectly equivalent, and sticking to node.js assert limits your dependency list. 它们是完全等价的,并坚持node.js断言限制你的依赖列表。

Disclaimer: I am the author of the assertthat module that I will refer to in this answer. 免责声明:我是断言这个模块的作者,我将在这个答案中提到。

Basically, you can achieve all the things with Node's very own assert module that you can do with all the other modules out there, such as Should.js , expect.js or assertthat . 基本上,你可以使用Node自己的assert模块实现所有的功能,你可以使用其他所有模块,例如Should.jsexpect.jsassertthat Their main difference is the way of how you can express your intent. 它们的主要区别在于如何表达您的意图。

Semantically speaking, the following lines of code are all equivalent to each other: 从语义上讲,以下几行代码都是相同的:

assert.areEqual(foo, bar);
foo.should.be.equal(bar);
expect(foo).to.be(bar);
assert.that(foo).is.EqualTo(bar);

Syntactically, there are two major differences: 从语法上讲,有两个主要区别:

First, the should syntax only works if foo is not equal to null or undefined , hence it's inferior to the other ones. 首先,只有当foo不等于nullundefinedshould语法才有效,因此它不如其他语法。 Second, there is a difference in readability: While assert.that(...) reads like natural language, all the others don't. 其次,可读性存在差异:虽然assert.that(...)读取的内容与自然语言相似,但其他所有内容都不然。

After all, Chai is only a wrapper around a few assertion modules to make things easier for you. 毕竟,Chai只是几个断言模块的包装器,可以让您更轻松。

So, to cut a long story short: No, there is no technical reason why to prefer one over the other, but readability and null compatibility may be reasons. 因此,简而言之:不,没有技术原因可以优先选择其中一个,但可读性和null兼容性可能是原因。

I hope this helps :-) 我希望这有帮助 :-)

PS: Of course, internally, they may be implemented differently, so there may be subtle things eg, how equality is checked. PS:当然,在内部,它们可能以不同的方式实现,因此可能存在微妙的事情,例如,如何检查相等性。 As said in the disclaimer, I'm the author of assertthat so I may be biased, but in the last few years I had the situation from time to time where assertthat was more reliable than the other ones, but as said, I may be biased. 正如免责声明中所述,我是断言的作者,所以我可能有偏见,但在过去的几年里,我不时地遇到这样的情况,断言比其他人更可靠,但正如所说,我可能是失之偏颇。

Since noone mentioned it, I thought I would mention rockstar programmer Guillermo Rauch's article (link is to Web Archive backup) on why you should avoid expect-style frameworks in favor of plainer assert style testing. 由于没有人提到它,我想我会提到摇滚明星程序员Guillermo Rauch的文章 (链接到Web存档备份),为什么你应该避免期望风格的框架支持更简洁的assert风格测试。

Mind you, he is the author of expect.js , so he has once thought otherwise. 请注意,他是expect.js的作者,所以他曾经想到过。 So have I. 我也有。

He makes an elaborate argument, but basically it's about reducing the mental burden of API overload. 他做了一个详尽的论证,但基本上是关于减少API重载的精神负担。 I can never remember which dialect of should and expect I am writing. 永远不会记得应该使用哪种方言,并期待我写作。 Was it .includes(foo).to.be.true() or was it .includes(foo).to.be.true or was it ... 它是.includes(foo).to.be.true()还是它.includes(foo).to.be.true还是它......

TJ Holowaychuck wrote a nice assert library called better-assert to get better output which you might check out . TJ Holowaychuck编写了一个很好的断言库,名为better-assert以获得更好的输出,你可以查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM