javascript 专门支持测试异步代码的 node.js 单元测试框架?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5370222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:58:44  来源:igfitidea点击:

Unit testing framework for node.js that specifically supports testing async code?

javascriptunit-testingnode.js

提问by alienhard

There are 30+ testing frameworks listed on the node.js module list...

node.js 模块列表中列出了 30 多个测试框架......

Which frameworks are most popular? And what are their pros and cons?

哪些框架最受欢迎?它们的优缺点是什么?

I'm looking for a framework that supports unit testing of code that has a lot of async callbacks (probably like most other node projects). Also, I'm looking for something rather simple and small than complex.

我正在寻找一个支持对具有大量异步回调的代码进行单元测试的框架(可能像大多数其他节点项目一样)。此外,我正在寻找比复杂更简单、更小的东西。

So far I've looked into:

到目前为止,我已经研究过:

  • Nodeunitis straightforward and supports async code by letting you define how many assertions you expect to be called and when a test is done.
  • Vowsjslooks interesting. Allows you to nicely structure the tests and comes with many advanced features. Not sure whether these features are actually useful, through, or if they rather stand in your way?
  • Nodeunit很简单,并且通过让您定义期望调用的断言数量以及测试完成时间来支持异步代码。
  • Vowsjs看起来很有趣。允许您很好地构建测试并带有许多高级功能。不确定这些功能是否真的有用,通过,或者它们是否会阻碍您?

回答by masylum

expressoand vowsare the most popular options. Although there are many good options out there. Update:As of Nov./2011, Mochais the successor to expresso, according to the Readme.md at the expressorepository. should.jsis often used with Mochato allow BDD-style assertions.

expresso誓言是最受欢迎的选择。虽然那里有很多不错的选择。更新:截至 2011 年 11 月,根据expresso存储库中的 Readme.md,Mochaexpresso的继承者。should.js通常与Mocha一起使用以允许 BDD 风格的断言。

Whatever you choose, I recommend using gentlyto stub core or third party modules.

无论您选择什么,我都建议轻轻地使用存根核心或第三方模块。

I use/maintain testosterone, a little testing framework that runs test on serial so you can abuse gently. Not so many people using it though.

我使用/维护testosterone,这是一个小测试框架,可以在串行上运行测试,因此您可以轻轻地滥用。虽然使用它的人并不多。

回答by a paid nerd

Since March 2012 we've been using Nodeunitand haven't looked back. It's straightforward and easy to work with, it lets you choose between module-level and class-level fixtures (setup/teardown), it's got a reasonable set of built-in assertions, and its command-line tool is very flexible. Try combining it with nodemon, too.

自 2012 年 3 月以来,我们一直在使用Nodeunit并且没有回头。它直接且易于使用,它允许您在模块级和类级装置(设置/拆卸)之间进行选择,它有一组合理的内置断言,并且它的命令行工具非常灵活。也尝试将其与nodemon结合使用。

We looked a Vows briefly but didn't want a whole new weird syntax for defining tests. All you need are assertions, fixtures, and a decent harness, and Nodeunit fits the bill perfectly.

我们简要地看了一个誓言,但不想要一个全新的奇怪语法来定义测试。您所需要的只是断言、固定装置和合适的线束,而 Nodeunit 完全符合要求。

回答by Dave Jensen

I haven't used it yet but what about Jasminefrom Pivotal Labs?

我还没有使用过它,但是Pivotal Labs 的Jasmine怎么样?

回答by Andrea

QUnitis as simple as a testing framework can get, and support asynchronous calls. It is the testing tool for JQuery, so you can be sure it is highly supported and works well.

QUnit就像测试框架一样简单,并且支持异步调用。它是 JQuery 的测试工具,因此您可以确定它受到高度支持并且运行良好。

You can consider integration with Sinon, which is a mocking framework, which in particular allows you to fake asynchronous events. This tutorialand the other parts of the tutorial linked there show how to use Sinon to test asynchronous code. It is on the browser, but you should be able to adapt it to testing node applications.

您可以考虑与Sinon集成,这是一个模拟框架,特别是允许您伪造异步事件。本教程和此处链接的教程的其他部分展示了如何使用 Sinon 测试异步代码。它在浏览器上,但您应该能够使其适应测试节点应用程序。

回答by greg.kindel

Minor plug for my own library, but more importantly I just want other harness developers to think about what a async testing in JS should look like:

我自己的库的小插件,但更重要的是,我只想让其他线束开发人员考虑 JS 中的异步测试应该是什么样子:

I wrote specifically OKJSbecause QUnit was so useless asynchronous. QUinit fails to catch exceptions (!), and requires a hacky start() for keeping track of depth.

我专门写了OKJS,因为 QUnit 是如此无用的异步。QUinit 无法捕获异常 (!),并且需要一个 hacky start() 来跟踪深度。

Many of the test frameworks out there fail to have a simple callback wrapper for testing timeouts and catching exceptions. I'm not sure why --callbacks are at the heart of asynchronous development. =/

许多测试框架都没有一个简单的回调包装器来测试超时和捕获异常。我不确定为什么 --callbacks 是异步开发的核心。=/

  setTimeout(function() {
    ok( true, "Passed and ready to resume!" );
    start();
  }, 1000);

OKJS catches exceptions in event listeners, and keeps track of outstanding async requests with a interal dead-man's trigger which can fail the test.

OKJS 在事件侦听器中捕获异常,并使用内部死人触发器跟踪未完成的异步请求,该触发器可能会导致测试失败。

  setTimeout( unit.callback("test callback is fired"), 500);
  setTimeout( unit.callback("callback fired, with subtests", function () {
        unit.assert("in callback, true === true", true,true);
  }),  500);

Would love to see this style support out of the enterprise solutions. Some of the other features OKJS advocates for are test blocks that execute in series (recipe style), but wait between blocks for events to be resolved.

希望从企业解决方案中看到这种风格的支持。OKJS 提倡的其他一些功能是串行执行的测试块(配方风格),但在块之间等待事件被解决。

回答by Dale Anderson

test-studiois an npm package that provides a powerful, web based front end for unit testing. It currently only supports mocha and provides things like executing individual or groups of tests and stepping node-inspector into individual tests.

test-studio是一个 npm 包,它为单元测试提供了一个强大的、基于 Web 的前端。它目前仅支持 mocha 并提供诸如执行单个或一组测试以及将 node-inspector 步进到单个测试之类的功能。

Read more about it here.

在此处阅读更多相关信息。