In association with heise online

Configuring Mocha

Mocha knows a large number of command-line parameters, but only a few of them are used on a regular basis. For obvious reasons, it is advisable to create an individual mocha.opts template file that can be used as a basis for each application.

  • --recursive causes subdirectories of the test directory to be included when searching for JavaScript files, which allows test files to be structured in a much clearer way.
  • --reporters lists all of Mocha's available output formats with a short description.
  • --reporter activates one of these output formats. The name of the required output format must be appended to the parameter as an option.
  • --interfaces lists all of Mocha's available test styles such as TDD and BDD.
  • --ui activates one of the test styles, and the name of this style must be appended as before. If no style is appended, Mocha will use BDD by default.
  • --bail aborts test execution after the first failed test. If this parameter is not included, Mocha will execute all tests and list any failed ones in their order of appearance.
  • --grep limits the number of tests to be executed to those whose names match a specific pattern. The pattern must be appended to the parameter as an option.
  • --slow and --timeout define limit values for slow and unresponsive tests in milliseconds. The required value must be appended to each parameter as an option.

A typical template for the mocha.opts file could contain the following code:

--recursive
--ui tdd
--reporter spec
--bail

Mocha in the web browser

Finally, Mocha can be used to test client-side code and execute tests within a web browser. The required initialisation code is slightly more involved than under Node.js. However, as it tends to look the same for each application, the code can be copied from a template that only needs to be generated once, like the contents of the mocha.opts file.

To execute Mocha in a web browser, the mocha.js and mocha.css files must first be downloaded and then integrated into an HTML page. The two files can be found in Mocha's GitHub repository: mocha.jsDirect download, mocha.cssDirect download. A module that will handle the assert phase is also required. The same procedure applies for this module.

When used in a browser, Mocha will generate its output within a div element that must be given the mocha ID. Therefore, the basic framework of a web page for executing Mocha-based client-side unit tests looks like this:

<!doctype html>
<html>
<head>
<title>Unit-Tests</title>
<link rel="stylesheet" type="text/css" href="mocha.css" />
<script type="text/javascript" src="mocha.js"></script>
<script type="text/javascript">
mocha.setup({
ui: 'tdd'
});
</script>
<script type="text/javascript" src="expect.js"></script>

    <!-- Systems under test go here -->
<script type="text/javascript" src="foo.js"></script>
<script type="text/javascript" src="[...].js"></script>

    <!-- Unit tests go here -->
<script type="text/javascript" src="fooTests.js"></script>
<script type="text/javascript" src="[...]Tests.js"></script>
</head>
<body>
<div id="mocha"></div>
<script type="text/javascript">
mocha.run();
</script>
</body>
</html>

The actual tests will be written in the same way as those for Node.js. Combining Mocha with the Require.js file and module loader is particularly interesting; with this combination, the same index.html file can be used for each application, and each code segment that is to be tested will be referenced as a dependency in the individual tests.

Conclusion

Mocha is an easy-to-use but powerful test framework that allows unit tests to be carried out under Node.js as well as within a web browser. Its various supported test styles such as TDD and BDD and its well-conceived test structuring and organisational options make Mocha one of the best unit testing frameworks for JavaScript. Its simple approach in terms of testing asynchronous code is a particularly interesting feature. The added fact that it gives developers ample freedom of choice in various places provides enough flexibility to cater for different philosophies without difficulty.


Golo Roden is the founder and CEO of "the native web", a company that specialises in native web technologies. He prefers JavaScript and Node.js for developing modern web applications and is the author of "Node.js & Co.", the first German-language book on this topic.


Print Version | Permalink: http://h-online.com/-1829727
  • Twitter
  • Facebook
  • submit to slashdot
  • StumbleUpon
  • submit to reddit
 


  • July's Community Calendar





The H Open

The H Security

The H Developer

The H Internet Toolkit