4. Mocha - Usage
-h, --help output usage information
-V, --version output the version number
-c, --colors force enabling of colors
-C, --no-colors force disabling of colors
-r, --require <name> require the given module
-w, --watch watch files for changes
--compilers <ext>:<module>,... use the given module(s) to compile files
--compilers ts:espower-typescript/guess
--recursive include sub directories
…
5. Example
var assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
6. Hooks
describe('hooks', function() {
before(function() {
// runs before all tests in this block
});
after(function() {
// runs after all tests in this block
});
beforeEach(function() {
// runs before each test in this block
});
afterEach(function() {
// runs after each test in this block
});
// test cases
});
7. assert api
assert(expression, message)
.ok(object, [message])
assert('foo' !== 'bar', 'foo is not bar');
assert(Array.isArray([]), 'empty arrays are arrays');
assert.ok('everything', 'everything is ok'); assert.ok(false,
'this will fail');
10. power-assert
provides descriptive assertion messages through standard assert interface.
available via npm and bower.
supports ES6 through babel plugin, module loader for Traceur Compiler,
and module loader for Babel.
supports CoffeeScript.
supports TypeScript.
has TypeScript type definition
11. power-assert
安裝:npm install mocha –save-dev
var assert = require('assert');
↓
var assert = require(‘power-assert’);