read

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:

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:

$ 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.


Image

@samwize

¯\_(ツ)_/¯

Back to Home