RUNNING MOCHA IN THE BROWSER

RUNNING MOCHA IN THE BROWSER

Mocha runs in the browser. Every release of Mocha will have new builds of ./mocha.js and ./mocha.css for use in the browser.

BROWSER-SPECIFIC METHODS

The following method(s) only function in a browser context:

mocha.allowUncaught() : If called, uncaught errors will not be absorbed by the error handler.

A typical setup might look something like the following, where we call mocha.setup(‘bdd’) to use the BDD interface before loading the test scripts, running them onload with mocha.run().




Mocha Tests










GREP

The browser may use the –grep as functionality. Append a query-string to your URL: ?grep=api.

BROWSER CONFIGURATION

Mocha options can be set via mocha.setup(). Examples:

// Use “tdd” interface. This is a shortcut to setting the interface;
// any other options must be passed via an object.
mocha.setup(‘tdd’);

// This is equivalent to the above.
mocha.setup({
ui: ‘tdd’
});

// Use “tdd” interface, ignore leaks, and force all tests to be asynchronous
mocha.setup({
ui: ‘tdd’,
ignoreLeaks: true,
asyncOnly: true
});
BROWSER-SPECIFIC OPTION(S)

The following option(s) only function in a browser context:

noHighlighting: If set to true, do not attempt to use syntax highlighting on output test code.

MOCHA.OPTS

Back on the server, Mocha will attempt to load ./test/mocha.opts as a configuration file of sorts. The lines in this file are combined with any command-line arguments. The command-line arguments take precedence. For example, suppose you have the following mocha.opts file:

–require should
–reporter dot
–ui bdd
This will default the reporter to dot, require the should library, and use bdd as the interface. With this, you may then invoke mocha with additional arguments, here enabling Growl support, and changing the reporter to list:

$ mocha –reporter list –growl
THE TEST/ DIRECTORY

By default, mocha looks for the glob ./test/*.js and ./test/*.coffee, so you may want to put your tests in test/ folder.

Questions