Mocha - the Most Popular Testing Framework for Node

Mocha is a simple yet wonderfully designed testing framework for Node.js.

In agile development, developers write tests before implementing a feature. When you are discussing a story for a feature, you should already be writing the test cases.

Here is a guide by Brian Stoner. After installing mocha and such, you can create a MakeFile like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
REPORTER = dot

test:
  @NODE_ENV=test ./node_modules/.bin/mocha \
    --reporter $(REPORTER) \

test-w:
  @NODE_ENV=test ./node_modules/.bin/mocha \
    --reporter $(REPORTER) \
    --growl \
    --watch

.PHONY: test test-w

(In case you wonder what .PHONY is..)

You can run your tests with:

1
$ make test

Another javascript library you should use together with mocha is should.js.

Should.js replaces assert library with more human-like language eg. foo.should.be.exactly(5)

An interesting thing to note: should.js extend object prototype!! While this is usually dangerous, should.js extend object with a should, and only that, thus safer.

Comments