Apologies, but I've been writing more code than blogs recently. My attention has been focused on Node recently and I've done some cool things:
my first npm package,
custom build environments for Sublime Text 3 (JSHint and Node Inspector), and a bunch of stuff I can't think of right now. Sure, I've been a crappy blogger, but I've been learning constantly. Since I would rather be a badass JS developer than a badass blogger, that's OK.
For convenience, I've been adding notes to the README.md file in the projects on Github. This is much easier than the whole blog thing. But, for you, my imaginary reader, I'm going to attempt to copy the html here.
update: Blogger doesn't want to play nicely with the Markdown styles, so I suggest reading it here.
Learn Mocha
This is a simple project to learn about
Mocha.
Notes
Set Up
Start by initializing npm.
npm init
Walk through the steps to get a basic
package.json
.
Add mocha to the package.
Add package dependencies through the console for convenience.
npm install mocha --save-dev
This adds it to package.json under "devDependencies".
Set up an npm script in package.json to test using the installed Mocha.
"scripts": {
"test": "node_modules/.bin/mocha"
},
Add
test/test.js
to your project. This is
where Mocha looks for tests by default.
If no file is specified when calling mocha, all *.js files in the ./test directory will be run. You can make it search recursively through the test dir with
--recursive
.
Now, running this from the console will execute the test cases in
./test
.
npm test
This executes the "test" defined in
package.json's "scripts".
Writing Tests
This has just wired up Mocha so far, which is the testing framework. The test(s) need to specify an
assertion library to use. You can use
Node's or anything else that throws errors (ie. write your own).
Asynchronous Tests
To make a test async, use the following format.
it('should work asynchronously', function (done) {
// do some assertions
done();
});
If you're testing something that returns a promise, use
it('should work asynchronously', function () {
return stuff().should.eventually.be(true);
});
Pending Test Cases
When you're not sure how to write the assertion for a test case, you can leave it out. This will show the test as "pending" when run.
it('is a pending test case');
Only
When you just want to run one suite or test, add
only
.
describe.only('only this suite will run', function () {
it('should test stuff');
it('should test other stuff');
});
Specify a Reporter
You just get the dot-matrix reporter by default. You can specify others with
--reporter foo
. Add this to the package.json "test" entry. You can see the available reporters with
./node_modules/.bin/mocha --reporters
Watch Files for Changes
--watch
watches for changings to the test file and any .js file in cwd. This is sweet.
Using a Makefile
According to Mocha's best practices, it's a good idea to "add a make test target to your Makefile". I'm not in the habit of providing a Makefile, but it's not that bad.
# The name of the file make will try to generate.
# If "test" already exists, make will not run the
# "recipe" for it.
test:
# This is a "recipe", the commands used to create
# the file "test".
./node_modules/.bin/mocha --reporter nyan
# Indicate that "test" is not a file to be created,
# but a task to execute. This means it will always
# run the "recipe", even if "test" already exists.
.PHONY: test
From the terminal, you can now run
make test
and it will run the "recipe" specified above. For that matter, you can just run
make
since it's the only thing in there.
(make ref)
References