Easy testing using mocha and assert by Harald Rudell
The Mocha Wrapper module makes testing with mocha and assert very easy.
- Find untested code via the code coverage report
- Get Immediate feedback by rerunning tests as you save
- Run smoke test before code push
npm testto avoid bad pushes
- Get testing immediately using One liner
- Ability to run coverage tests anytime without code changes
- Package consistency test looks for frequently forgotten items
- Automated adding of mocha to your project
- Enhanced assertion printouts with message, actual and expected values
- Abbreviated stack traces compared to the underlying framework
- Resilience against syntax issues in test files
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
- Linux has a jscoverage package:
sudo apt-get install jscoverage
- Windows: jsCoverage site has zip to download
sudo macports install jscoverage
- There is a github node-jscoverage if you have a C compiler installed.
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
- Mochawrapper wraps and extends mocha, a testing framework by TJ Holowaychuk
No warranty expressed or implied. Use at your own risk.
Please suggest better ways, new features, and possible difficulties on github