-
Notifications
You must be signed in to change notification settings - Fork 3
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.
-
install mochajs by
npm install -g mocha -
configure your testing env
- make a test folder by
mkdir test - make a env file called
mocha.optsunder foldertestby
cd test touch mocha.opts- configure your env with
- make a test folder by
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
```
You can get synchronous examples in MochaJs, which is very obvious how it works.
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_titleandpromise_get_titlein a file underpro_for_testfolder, in our case, which is in the same level as foldertest.
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
testfolder.
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();
}
)
});
});