GEETING STARTED

  1. $ npm install mocha
  2. $ mkdir test
  3. $ $EDITOR test/test.js # 或者使用你喜欢的编辑器

在编辑器中输入:

  1. var assert = require('assert')
  2. describe('Array', function() {
  3. describe('#indexOf()', function() {
  4. it('should return -1 when the value is not present', function() {
  5. assert.equal(-1, [1, 2, 3].indexOf(4))
  6. })
  7. })
  8. })

然后在终端中运行:

  1. $ ./node_modules/mocha/bin/mocha
  2. Array
  3. #indexOf()
  4. should return -1 when the value is not present
  5. 1 passing (9ms)

package.json中设置一个测试脚本:

  1. "scripts":{
  2. "test": "mocha"
  3. }

然后运行:

  1. $ npm test