Skip to content

Latest commit

 

History

History
243 lines (177 loc) · 6.89 KB

Readme.md

File metadata and controls

243 lines (177 loc) · 6.89 KB

mocha

Mocha aims to combine the best of several popular JavaScript test frameworks, providing a fun, accessible, robust browser & node.js based test experience.

About

Mocha tests run serially, easing debugging and making it an ideal choice when mocking and stubbing is involved. Existing frameworks such as expresso can be much faster, though not without cost, Mocha aims to be the simple and "fun" test framework.

Features

  • proper exit status for CI support etc
  • ideal for asynchronous APIs
  • auto-detects and disables coloring for non-ttys
  • maps uncaught exceptions to the correct test case
  • async test timeout support
  • growl notification support
  • reports test durations
  • highlights slow tests
  • global variable leak detection
  • configurable test-case timeout
  • optionally run tests that match a regexp
  • auto-exit to prevent "hanging" tests
  • easily meta-generate suites & test-cases
  • extensible reporting
    • dot matrix
    • landing strip
    • test-anything-protocol (TAP) producer
    • progress bar
    • specification listing
    • hierarchical specification
    • streaming JSON
    • JSON
  • extensible test DSLs
    • BDD
    • TDD
    • exports

Usage


Usage: mocha [options] <files>

  Options:

    -h, --help             output usage information
    -v, --version          output the version number
    -r, --require <name>   require the given module
    -R, --reporter <name>  specify the reporter to use
    -u, --ui <name>        specify user-interface (bdd|tdd|exports)
    -g, --grep <pattern>   only run tests matching <pattern>
    -t, --timeout <ms>     set test-case timeout in milliseconds [2000]
    -G, --growl            enable growl support

  Reporters:

    dot - dot matrix
    json - single json object
    progress - progress bar
    list - spec-style listing
    tap - test-anything-protocol
    landing - unicode landing strip
    json-stream - newline delimited json events

Interfaces

Mocha "interfaces" providing BDD, TDD, and expresso export-style flavoured APIs on top of the internals.

BDD

describe('Array', function(){
  before(function(){
    // ...
  });

  describe('#indexOf()', function(){
    it('should return -1 when not present', function(){
      [1,2,3].indexOf(4).should.equal(-1);
    });

    it('should return the index when present', function(){
      [1,2,3].indexOf(3).should.equal(2);
      [1,2,3].indexOf(2).should.equal(1);
      [1,2,3].indexOf(1).should.equal(0);
    });
  });
});

TDD

suite('Array', function(){
  setup(function(){
    // ...
  });

  suite('#indexOf()', function(){
    test('should return -1 when not present', function(){
      assert.equal(-1, [1,2,3].indexOf(4));
    });

    test('should return the index when present', function(){
      assert.equal(2, [1,2,3].indexOf(3));
      assert.equal(1, [1,2,3].indexOf(2));
      assert.equal(0, [1,2,3].indexOf(1));
    });
  });
});

Exports

module.exports = {
  'Array': {
    '#indexOf()': {
      'should return -1 when not present': function(){
        [1,2,3].indexOf(4).should.equal(-1);
      },
      
      'should return the index when present': function(){
        [1,2,3].indexOf(3).should.equal(2);
        [1,2,3].indexOf(2).should.equal(1);
        [1,2,3].indexOf(1).should.equal(0);
      }
    }
  }
};

Reporters

Mocha reporters adjust to the terminal window, and always disable ansi-escape colouring when the stdio streams are not associated with a tty.

Dot Matrix

The Dot Matrix reporter is simply a series of dots that represent test cases, failures highlight in red.

dot matrix reporter

dot matrix failure

TAP

The TAP reporter emits lines for a Test-Anything-Protocol consumer.

test anything protocol

Landing Strip

The Landing Strip reporter is a gimmicky test reporter simulating a plane landing :) unicode ftw

landing strip plane reporter

List

The "List" reporter outputs a simple specifications list as test cases pass or fail, outputting the failure details at the bottom of the output.

list reporter

failures

JSON

The JSON reporter outputs a single large JSON object when the tests have completed (failures or not).

JSON Stream

The JSON Stream reporter outputs newline-delimited JSON "events" as they occur, beginning with a "start" event, followed by test passes or failures, and then the final "end" event.

["start",{"total":12}]
["pass",{"title":"should return -1 when not present","fullTitle":"Array #indexOf() should return -1 when not present","duration":0}]
["pass",{"title":"should return the index when present","fullTitle":"Array #indexOf() should return the index when present","duration":0}]
["fail",{"title":"should return -1 when not present","fullTitle":"Array #indexOf() should return -1 when not present"}]
["end",{"start":"2011-08-29T03:21:02.050Z","suites":13,"passes":11,"tests":12,"failures":1,"end":"2011-08-29T03:21:02.052Z","duration":2}]

Running tests

Run mocha tests:

$ make test

Run all tests, including interfaces:

$ make test-all

Alter the reporter:

$ make test REPORTER=list

Best practices

Makefiles

Be kind and don't make developers hunt around in your docs to figure out how to run the tests, add a make test target to your Makefile:

test:
  ./node_modules/.bin/mocha \
    --reporter list

.PHONY: test

License

(The MIT License)

Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.