Node.js unit testing [closed]
Asked Answered
C

10

170

Are there any good Node.js (server-side JavaScript) unit testing frameworks currently out there?

I'm looking for something a little deeper than the provided Assert module.

Cramp answered 31/8, 2011 at 7:50 Comment(0)
L
66

I ended up using Nodeunit and am really happy with it.

I was using Expresso originally, but the fact that it runs tests in parallel caused a few problems. (For example, using database fixtures doesn't work well in this situation.)

Legator answered 31/8, 2011 at 9:35 Comment(6)
Expresso doesn't force you to run tests in parallel. Feed it the --serial argument and it'll run them all in merry order.Dentifrice
Just downloaded and used nodeunit... does exactly what it says, worked first time ftw!Golub
Nodeunit is a bit strange with requiring every test to call .done() imho.Marucci
If I was starting a new project now I'd probably use Mocha as the test frameworkLegator
Nodeunit requires .done() to accommodate asynchronous testing. I agree it can be a little unwieldy at times, but it has a mighty fine reason for doing it this way.Rarebit
node-unit seems to be deprecated.Birdwatcher
J
49

I was also looking for a decent test framework for node and found Mocha. It is the official successor to Expresso and seems very mature.

It allows to plug-in different assertion libraries, it offers reporters for code coverage and other things (you can plug-in your own). It can run synchronously or asynchronously and it has a concise API.

I will give it a try and report back...

EDIT:

After an incredible amount of time dedicated to other projects I finally came back to a JavaScript project and had time to play around with mocha. I can seriously recommend using it. The tests read very nicely, integration with Gulp.js is great and tests run very fast. I was able to setup automatic standalone as well as in-browser (Browserify) test runs and corresponding code coverage reports in about half a day (most of the time spent on understanding how to use Browserify from Gulp.js). To me, Mocha seems a very good choice for a testing framework.

UPDATE:

I am still very convinced about Mocha. Integration with Chai allows to plugin different assertion styles. You can checkout a working setup in this GitHub project. I am using it with karma now, integrating code coverage report, automatic watchers and good integration with IntelliJ IDEA.

Jellied answered 6/3, 2013 at 10:57 Comment(1)
Getting started with Mocha - brianstoner.com/blog/testing-in-nodejs-with-mochaPerri
D
15

Personally I've stuck with Expresso, but there are a bunch of different frameworks out there, accommodating most testing styles.

Joyent has an extensive list; give that a go.

Dentifrice answered 31/8, 2011 at 7:54 Comment(1)
Expresso is the only one which makes sense for me, and works with asynchronous code properly. Tried vows and qunit too.Potaufeu
V
15

I've personally only used the assert module, but I also find myself wanting more. I've looked through many Node.js modules and popular unit testing frameworks are Nodeunit and should (which is made by the same guy as Espresso (maybe an updated name?)

Vows also looks promising.

Versicle answered 31/8, 2011 at 8:16 Comment(1)
I upvote you just for that pun.Komsomolsk
P
5

vows is a solid unit testing library for Node.js, but the syntax is tedious.

I've written a thin abstraction called vows-fluent which makes the API chainable.

And I've written another abstraction, [vows-is] which builds on vows-fluent and exposes a BDD style syntax.

An example would be

var is = require("vows-is");

is.suite("testing is fun").batch()

    .context("is testing fun?")
        .topic.is("yes")
        .vow.it.should.equal("yes")

.suite().run({
    reporter: is.reporter
});

More examples.

Phalansterian answered 31/8, 2011 at 12:57 Comment(5)
amen to that, vows syntax is horrendous IMOCowitch
Had a play with vows with the appeasy wrapper. Looked like a good idea for testing a json api but rapidly came up short. I also tend to find that apis which are heavily orientated towards chaining calls like this are difficult to use and hard to debug. They look good in examples, but don't work well under real world usage.Maloriemalory
@Maloriemalory vows and vows-is are horrible. use mochaPhalansterian
Trying it now, but appear to be running into a similar bunch of issues.Maloriemalory
you must be using it wrong then. try reading gist.github.com/2896455. @MaloriemaloryPhalansterian
F
4

I think among various testing frameworks available, Mocha is the most latest, and very simple to implement. Here is a wonderful tutorial about how to use it:

How to build and test your Rest API with Node.js, Express and Mocha

Foreordain answered 17/10, 2013 at 6:1 Comment(1)
Well, thanks for your compliment about the tutorial :)Galitea
O
1

If you are familiar with QUnit, you could use node-qunit which is a sort of a node wrapper around QUnit's existing framework.

Ochone answered 4/2, 2013 at 0:37 Comment(0)
S
0

Originally made for Node.js, deadunit is a JavaScript unit testing library for Node.js and the browser. Some of its unique attributes:

  • Easy learning curve
  • Can output test results on the command line (colored or plain-text) or as HTML
  • It prints out the actual lines of code where your assertions are, so your output makes sense even if you don't spend a lot of time writing test commentary
  • It has a simple count assertion that makes dealing with expected exceptions and asynchronous asserts easy
  • it prints out exception and any attached data they have
  • it'll let you know if your code is hanging (something you don't want, but usually goes unnoticed)
  • Has an event driven API enables streaming test results across a network, or in any way you want.
  • Supports testing with node-fibers
Soilasoilage answered 11/1, 2014 at 5:10 Comment(0)
H
0

test-studio is an npm package that provides a powerful, web based front end for unit testing. It supports things like executing individual or groups of tests and stepping node-inspector into individual tests. It currently supports mocha and more frameworks will be supported in future given demand.

Read more about it here.

Disclaimer: I am the author.

Handmedown answered 18/5, 2014 at 1:40 Comment(1)
looks like this was written by you, right? if so, you should disclose that fact in your answer.Newman
L
0

I just uploaded a project I am using to unit test Node.js with Karma and Jasmine: Narma.

Your Node.js modules get loaded into a node-webkit browser, so you can execute Node.js modules and use libraries like jQuery in the same heap.

Layfield answered 5/3, 2015 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.