Easy testing using mocha and assert by Harald Rudell
The Mocha Wrapper module makes testing with mocha and assert very easy.
npm testto avoid bad pushes
Simply add mocha :)
npm install mochawrapper; node_modules/bin/addmocha; npm test
> firstname.lastname@example.org test /home/foxyboy/Desktop/c505/node/mochawrapper> mocha --ui exports --reporter spec...
addmocha command updates .gitignore, package.json and the test folder in a safe manner so testing can start immediately from a single dependency.
Imagine yourself, at blood, toil, tears and sweat, supported by the sweet mocha every time you save your work:
npm run-script monitor
A project using mochawrapper is tagfinder:
git clonecd tagfindernpm installnpm testnpm run-script coverage
The system’s browser displays the test coverage report for tagfinder.
npm run-script coverage displays a test coverage report by opening a tab or window in the system’s browser. There is no need to modify any code to run coverage, and you can continuously use it as a guide for further areas to test.
npm run-script monitor continuously displays current test results and rerun the tests as you save your sourcefiles.
npm test runs all the tests in the test folder.
The report can also be invoke using
node_modules/.bin/mochacoverage or simply
mochacoverage if you have updated your PATH. For both of these methods, you can add a top-level folder other than
The jsCoverage command is required.
jscoverage --versionjscoverage 0.5.1
sudo apt-get install jscoverage
sudo macports install jscoverage
node_modules/.bin to your PATH environment variable to run executable scripts from your modules.
Here are two tests from a test suite. The first test is regular code, and the second test features a callback.
var assert = require'mochawrapper'exports'Array Length:' =var expected = 'number'var actual = typeof lengthassertequalactual expectedsetTimeoutcompleteWhenThisExecutes 100console.log'background complete'console.log'finishing test'done
Save the tests as a .js file in the project's test folder.
© Harald Rudell wrote mochawrapper for node in August, 2012
No warranty expressed or implied. Use at your own risk.
Please suggest better ways, new features, and possible difficulties on github