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 test
to 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
> [email protected] test /home/foxyboy/Desktop/c505/node/mochawrapper
> mocha --ui exports --reporter spec
...
The 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
debugger:1 found in 'lib/passportwrapper.js'
Files checked for syntax: javascript:27, json:1 in 0.1 s
✔ 8 tests complete (150ms)
◡ watching
A project using mochawrapper is tagfinder:
git clone https://github.com/haraldrudell/tagfinder
cd tagfinder
npm install
npm test
npm 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.
npm run-script debugtest
runs all tests using the debugger. By inserting JavaScript’s debugger statement at strategic locations you can run to the exact location where there is something funky going on.
npm run-script coverage
mochacoverage Preparing an automated test coverage report
mochacoverage Invoking jsCoverage
mochacoverage Running tests
Files checked for syntax: javascript:9, json:1 in 0.1 s
mochacoverage Preparing report
mochacoverage Launching browser
mochacoverage Complete
This does coverage analysis of JavaScript files in the project's lib folder. When complete, Mocha Wrapper launches a new tab in the system default browser containing JavaScript source code. Lines marked red were not executed.
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 lib
.
The jsCoverage command is required.
jscoverage --version
jscoverage 0.5.1
- Linux has a jscoverage package:
sudo apt-get install jscoverage
- Windows: jsCoverage site has zip to download
- Mac:
sudo macports install jscoverage
- There is a github node-jscoverage if you have a C compiler installed.
npm test
> [email protected] test /home/foxyboy/Desktop/c505/node/tf
> mocha --ui exports --reporter spec
Empty Markup
✓ Can compile undefined
✓ Can compile empty string
Parsing
✓ Can remove html comments
✓ Can find opening tags
✓ Empty attributes
✓ Unquoted attributes
✓ Quoted attributes
✓ Unescaped content: script and textarea
✓ Closing tag in unescaped content
✓ MathML
✓ cdata section
✓ svg
Package Consistency:
◦ Proper JavaScript and json syntax: Files checked for syntax: javascript:3, json:1 in 0.1 s
✓ Proper JavaScript and json syntax (87ms)
✓ Package descriptor file
✓ git ignore declaration
✓ Readme
✔ 16 tests complete (102ms)
Add 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:'] = {
'array.length returns a number': function () {
var expected = 'number'
var actual = typeof [].length
assert.equal(actual, expected)
},
'Testing with callback (asynchronous)': function (done) {
setTimeout(completeWhenThisExecutes, 100)
console.log('background complete')
function completeWhenThisExecutes() {
console.log('finishing test')
done()
}
}
}
Save the tests as a .js file in the project's test folder.
npm test
> [email protected] test /home/foxyboy/Desktop/c505/node/cloudclearing
> mocha --ui exports --reporter spec
Array Length:
✓ array.length returns a number
◦ Testing with callback (asynchronous): background complete
finishing test
✓ Testing with callback (asynchronous) (111ms)
Package Consistency:
◦ Proper JavaScript and json syntax: Files checked for syntax: javascript:16, json:1 in 0.1 s
✓ Proper JavaScript and json syntax (79ms)
✓ Package descriptor file
✓ git ignore declaration
✓ Readme
✔ 7 tests complete (198ms)
© 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