Skip to content
PhoenixAndMachine edited this page Jul 8, 2014 · 15 revisions

MochaJs

Overview of MochaJs

MochaJs is a test tool for js, which is very handy to use. There is complete introduction for MochaJs. In this page, we will show how to install mocha, configure env with some practical examples.

Configure Testing Environment

  1. install mochajs by npm install -g mocha

  2. configure your testing env

    • make a test folder by mkdir test
    • make a env file called mocha.opts under folder test by
    cd test
    touch mocha.opts
    
    • configure your env with

Options:

-h, --help output usage information -V, --version output the version number -r, --require require the given module -R, --reporter specify the reporter to use -u, --ui specify user-interface (bdd|tdd|exports) -g, --grep only run tests matching -i, --invert inverts --grep matches -t, --timeout set test-case timeout in milliseconds [2000] -s, --slow "slow" test threshold in milliseconds [75] -w, --watch watch files for changes -c, --colors force enabling of colors -C, --no-colors force disabling of colors -G, --growl enable growl notification support -d, --debug enable node's debugger, synonym for node --debug -b, --bail bail after first test failure --recursive include sub directories --debug-brk enable node's debugger breaking on the first line --globals allow the given comma-delimited global [names] --ignore-leaks ignore global variable leaks --interfaces display available interfaces --reporters display available reporters --compilers :,... use the given module(s) to compile files **Example** in `mocha.opts` --require should --reporter spec --timeout 8000 --slow 300 --globals i ```

Test Synchronous

You can get synchronous examples in MochaJs, which is very obvious how it works.

Test Asynchronous

Example 1: Reading From A File Create a file under folder test with content.

var should = require('should');

var fs = require('fs');

describe('Read File Test', function() {
    it('Read existing file', function(done) {
	fs.readFile('./test/mocha.opts', function(errors, data) {
	    should.not.exist(errors);
	    done();
	});
    });

    it('Read none existing file', function(done) {
	fs.readFile('/doesnot/exist', function(errors, data) {
            should.exist(errors);
	    done();
	})
    });

    it('This is a pending test');
});

Example 2: Get Title From A Website We can define our own asynchronous function, and test it. Here we have an example to get tile from a website, and we make it with promiseJs as well. If you have problems of understanding promise, please read Intro PromiseJs

  • Make get_title and promise_get_title in a file under pro_for_test folder, in our case, which is in the same level as folder test.
var jsdom = require('jsdom');
var Q = require('q');

var get_title = function(url, callback) {
    jsdom.env(url, 
        ["http://code.jquery.com/jquery.js"],
	function(errors, window) {
	    if (errors) return callback(errors);
	    var title = window.$('title').text();
	    return callback(null, title)
	}
    );
};

var promise_get_title = function(url) {
    return Q.nfcall(jsdom.env, url, ["http://code.jquery.com/jquery.js"]).then(
	function(window) {
	    var title = window.$('title').text();
	    return title;
	},
	function(error) {
	    throw error;
	}
    );
};

module.exports.get_title = get_title;
module.exports.promise_get_title = promise_get_title;
  • Our tests in a file under test folder.
var should = require('should');

var get_title = require('../pro_for_test/get_title').get_title;
var promise_get_title = require('../pro_for_test/get_title').promise_get_title;

describe('GET_TITLE', function() {

    it('Google has title Google', function(done) {
	get_title("http://www.google.de", function(err, title) {
	    should.not.exist(err);
	    title.should.be.a('string');
	    title.should.equal('Google');
	    done();
	})
    });

    it('Google has title called Google', function(done) {
	promise_get_title("http://www.google.de").then(
	    function(title) {
		title.should.be.a('string');
		title.should.equal('Google');
		done();
	    }
	)
    });
});

Clone this wiki locally