开发者

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

开发者 https://www.devze.com 2023-02-17 19:50 出处:网络
There are 30+ testing frameworks listed on the node.js module list... Which frameworks are most popular? And what are their pros and cons?

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

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 projec开发者_开发问答ts). Also, I'm looking for something rather simple and small than complex.

So far I've looked into:

  • Nodeunit is straightforward and supports async code by letting you define how many assertions you expect to be called and when a test is done.
  • Vowsjs looks 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?


expresso and vows are the most popular options. Although there are many good options out there. Update: As of Nov./2011, Mocha is the successor to expresso, according to the Readme.md at the expresso repository. should.js is often used with Mocha to allow BDD-style assertions.

Whatever you choose, I recommend using gently to 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.


Since March 2012 we've been using Nodeunit and 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.

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.


QUnit is 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.

You can consider integration with Sinon, which is a mocking framework, which in particular allows you to fake asynchronous events. This tutorial and 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.


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


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:

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

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. =/

  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.

  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.


test-studio is 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.

Read more about it here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号