(Un)commonJS unit test runner
Implementation of (Un)commonJS unit test runner.
Testing
In order to make your package testable from npm you should:
- Create a directory in your package root.
- Define test directory in package descriptor under
directories
section. - Define test script in package descriptor under
scripts
section. - Define dependency on this package (It's name is "test" in npm registry).
- Write your tests
- Test your package by running all tests
npm test
or run individual testsnode ./path/to/test/group.js
Example
package.json
"name": "mypackage" "version": "0.7.0" "description": "Sample package" "scripts": "test": "node test/all.js" "devDependencies": "test": ">=0.0.5"
Async test
// if test function expects second named argument it will be executed// in async mode and test will be complete only after callback is calledexports'test my async foo' = { var http = var google = http var request = google request request} if module == requiremain
Sync test
// using assert passed to the test function that just logs failuresexports'test that logs all failures' = { assert assert} if module == requiremain
Fast fail
// using nodejs's build in asserts that throw on failurevar assert = exports'test that stops execution on first failure' = { assert assert} if module == requiremain
Custom assertions
var AssertBase = Assertvar AssertDescriptor = { if lower < inner && inner < upper this else this; } enumerable: true } { return Object} // bundling custom asserts with test suiteexportsAssert = Assertexports'test with custom asserts' = { assert assert} if module == requiremain
For more examples checkout tests for this package and for more details see the UncommonJS unit test runner specification.