131

I have looked at the rather long list of testing frameworks at https://github.com/ry/node/wiki/modules#testing. What is the experience with these frameworks?

Obviously the ability to run in the browser would be a big bonus, but I'm mainly interested in Node.js. Something with a heavily asynchronous slant would be great.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
doffm
  • 1,335
  • 2
  • 9
  • 3

6 Answers6

70

Update:

Mocha is the best in my opinion.


What is the experience with these frameworks?

I played with expresso which is pretty cool testing framework which also has test-coverage. It has been created by TJ Holowaychuk who is also the creator of Express.js (insanely fast (and small) server-side JavaScript web development framework built on Node.js and Connect). I recently saw that he also has a cool library called should.js which can be used together with Expresso for a even better testing experience.

Obviously, the ability to run in the browser would be a big bonus

I don't believe it can run in the browser, but I also don't get why you would want to run it inside the browser?

but I'm mainly interested in Node.js. Something with a heavily asynchronous slant would be great.

Quote from the expresso:

The argument passed to each callback is beforeExit, which is typically used to assert that callbacks have been invoked.

You can use beforeExit to test asynchronous functions.


TIP: Follow TJ Holowaychuk on GitHub, because he creates very good open-source code.

backslashN
  • 2,795
  • 3
  • 15
  • 25
Alfred
  • 60,935
  • 33
  • 147
  • 186
40

I use VowsJS which is easy to use async BDD framework (Behaviour Driven Development) and get the job done.

From what I see lately it's what many chose to test their NPM modules, so I believe so far it's one the best to use.

Some popular testing frameworks that could be used with NodeJS are also those:

You can also see a list of JavaScript test frameworks here

Few more libs that could help you write better code are those:

There is also Bamboo CI Server by Atlassian it automates builds and tests. It is a package for Apache/Tomcat (which sux because it uses Java and that makes it very heavy) also is not free but it has a starter license which costs $10 so I believe it is affordable. It is the most featured of all CI servers I found so far and it supports all unit tests that support xUnit that means that you can run builds/tests for any language with Bamboo.

Another option for CI with NodeJS is Travis which lot of people use for their open source projects, as it says A hosted continuous integration service for the open source community.

There is also a google group discussion with Continuous Integration for Node JS Projects topic.

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
panosru
  • 2,709
  • 4
  • 33
  • 39
  • 6
    A note for people thinking of using Vows: it hasn't been updated since 2012 – Code Commander Sep 17 '14 at 20:29
  • They [made some changes after](https://github.com/vowsjs/vows/blob/master/CHANGELOG.md). last release: Sep, 2015 – Andre Figueiredo Feb 22 '18 at 23:53
  • got bad gateway on the Vows oficial site in 2020, maybe dead? – Lincoln May 24 '20 at 20:00
  • wow, buddy, it's been A LOT of years since then, all I could find about VowJS now is this: http://istavros.github.io/vowjs/ but unfortunately, I cannot suggest you use it at 2020. It is outdated, and I would strongly suggest you check Mocha (https://mochajs.org/), Jasmin (https://jasmine.github.io/) and Jest (https://jestjs.io/) instead. – panosru May 25 '20 at 21:13
14

Based on the asker's comments above, I tried out vows, and it solved a lot of problems I was having with my async testing. Its ability to mix serial and parallel testing is awesome.

Make sure you read the guidance doc carefully, but once you get the hang of it, it's flexible, powerful, and produces nice, clean results.

UPDATE: I would also encourage people to check out should for their asserts. It allows for very flexible, very readable asserts, and is compatible with both Expresso and Vows, and probably most other test frameworks as well.

(I'm posting this as a separate answer just in case people don't notice the comments on Alfred's answer.)

UPDATE 1/7/2015: For what it's worth, I have since switched from Vows to Mocha, and from Should to Chai. Mocha has much better support now for asynchronous tests using promises, and Chai allows for several flexible assert options, including the expect api, for those who don't like to modify the object prototype.

Chris Jaynes
  • 2,868
  • 30
  • 29
  • 1
    `should` tacks a non-enumerable property named `should` to the `Object` prototype, meaning *all* of the values / objects you're dealing with look *slightly* different at test time and at production time. While this probably 'just works' in most cases, it's in principle a bad idea to modify built-in prototypes; to do it only during testing feels wrong. It's all been solely done so they can have a nice syntax. – flow Jan 07 '15 at 17:15
  • @flow since v2 it is easy to use `should` without extending `Object.prototype` (simply call `require('should').noConflict()` and use should.js as an expect alternative. – den bardadym Jan 14 '15 at 11:07
6

I've started using Jasmine for my JavaScript testing specifically because it's small and runs in both the browser and node. It's also got a really solid reporting and matcher API so it's easy to integrate with other tools in the future. Having a buildin mocking framework is also useful since it's often one of the first things I would add when I was using qunit for TDD in the browser.

rsp
  • 107,747
  • 29
  • 201
  • 177
Mark B
  • 2,870
  • 3
  • 20
  • 18
2

If you want a true BDD framework then maybe consider Yadda. It integrates with mocha, jasmine, nodeunit, qunit, zombie and casperjs, to support feature files, e.g.

   Scenario: provides the version of all services
      given service x is running
      and service y is running
      when I request the service versions
      then service x should be version 0.0.1
      and service y should be version 0.0.2
2

I've been using nodeunit and its ability to work with async functions is reasonably straightforward.

There's a nice walkthrough that should get you ready-to-go with nodeunit on his blog.

[ Note: the API has changed since the blogpost – setUp(callback) and tearDown(callback) both take a callback as an argument, which you need to call when your setup/teardown is complete. ]

MMM
  • 7,221
  • 2
  • 24
  • 42
Vanwaril
  • 7,380
  • 6
  • 33
  • 47
  • Looking at this and after attempting a few tests of mongoose.js functions in expresso, nodeunit's preference for not running all tests in parallel and permitting setUp and tearDown tests looks useful. – asparagino May 23 '12 at 16:05