Mocha Diff

DIFFS

Mocha supports the err.expected and err.actual properties of any thrown AssertionErrors from an assertion library. Mocha will attempt to display the difference between what was expected, and what the assertion actually saw. Here’s an example of a “string” diff:

string diffs

USAGE

Usage: mocha [debug] [options] [files]

Commands:

init initialize a client-side mocha setup at

Options:

-h, –help output usage information
-V, –version output the version number
-A, –async-only force all tests to take a callback (async) or return a promise
-c, –colors force enabling of colors
-C, –no-colors force disabling of colors
-G, –growl enable growl notification support
-O, –reporter-options reporter-specific options
-R, –reporter specify the reporter to use
-S, –sort sort test files
-b, –bail bail after first test failure
-d, –debug enable node’s debugger, synonym for node –debug
-g, –grep only run tests matching -f, –fgrep only run tests containing
-gc, –expose-gc expose gc extension
-i, –invert inverts –grep and –fgrep matches
-r, –require require the given module
-s, –slow “slow” test threshold in milliseconds [75]
-t, –timeout set test-case timeout in milliseconds [2000]
-u, –ui specify user-interface (bdd|tdd|qunit|exports)
-w, –watch watch files for changes
–check-leaks check for global variable leaks
–full-trace display the full stack trace
–compilers :,… use the given module(s) to compile files
–debug-brk enable node’s debugger breaking on the first line
–globals allow the given comma-delimited global [names]
–es_staging enable all staged features
–harmony<_classes,_generators,...> all node –harmony* flags are available
–preserve-symlinks Instructs the module loader to preserve symbolic links when resolving and caching modules
–icu-data-dir include ICU data
–inline-diffs display actual/expected differences inline within each string
–interfaces display available interfaces
–no-deprecation silence deprecation warnings
–no-exit require a clean shutdown of the event loop: mocha will not call process.exit
–no-timeouts disables timeouts, given implicitly with –debug
–opts specify opts path
–perf-basic-prof enable perf linux profiler (basic support)
–prof log statistical profiling information
–log-timer-events Time events including external callbacks
–recursive include sub directories
–reporters display available reporters
–retries set numbers of time to retry a failed test case
–throw-deprecation throw an exception anytime a deprecated function is used
–trace trace function calls
–trace-deprecation show stack traces on deprecations
–use_strict enforce strict mode
–watch-extensions ,… additional extensions to monitor with –watch
–delay wait for async suite definition
-w, –watch

Executes tests on changes to JavaScript in the CWD, and once initially.

–compilers

CoffeeScript is no longer supported out of the box. CS and similar transpilers may be used by mapping the file extensions (for use with –watch) and the module name. For example –compilers coffee:coffee-script with CoffeeScript 1.6- or –compilers coffee:coffee-script/register with CoffeeScript 1.7+.

About Babel

If your ES6 modules have extension .js, you can npm install –save-dev babel-register and use mocha –require babel-register; –compilers is only necessary if you need to specify a file extension.

-b, –bail

Only interested in the first exception? use –bail!

-d, –debug

Enables node’s debugger support, this executes your script(s) with node debug allowing you to step through code and break with the debugger statement. Note the difference between mocha debug and mocha –debug: mocha debug will fire up node’s built-in debug client, mocha –debug will allow you to use a different interface — such as the Blink Developer Tools.

–globals

Accepts a comma-delimited list of accepted global variable names. For example, suppose your app deliberately exposes a global named app and YUI, you may want to add –globals app,YUI. It also accepts wildcards. You could do –globals ‘*bar’ and it would match foobar, barbar, etc. You can also simply pass in ‘*’ to ignore all globals.

–check-leaks

By default, Mocha will not check for global variables leaked while running tests, to enable this pass –check-leaks, to specify globals that are acceptable use –globals, for example –globals jQuery,MyLib.

-r, –require

The –require option is useful for libraries such as should.js, so you may simply –require should instead of manually invoking require(‘should’) within each test file. Note that this works well for should as it augments Object.prototype, however if you wish to access a module’s exports you will have to require them, for example var should = require(‘should’). Furthermore, it can be used with relative paths, e.g. –require ./test/helper.js

-u, –ui

The –ui option lets you specify the interface to use, defaulting to “bdd”.

-R, –reporter

The –reporter option allows you to specify the reporter that will be used, defaulting to “spec”. This flag may also be used to utilize third-party reporters. For example if you npm install mocha-lcov-reporter you may then do –reporter mocha-lcov-reporter.

-t, –timeout

Specifies the test-case timeout, defaulting to 2 seconds. To override you may pass the timeout in milliseconds, or a value with the s suffix, ex: –timeout 2s or –timeout 2000 would be equivalent.

-s, –slow

Specify the “slow” test threshold, defaulting to 75ms. Mocha uses this to highlight test-cases that are taking too long.

-g, –grep

The –grep option when specified will trigger mocha to only run tests matching the given pattern which is internally compiled to a RegExp.

Suppose, for example, you have “api” related tests, as well as “app” related tests, as shown in the following snippet; One could use –grep api or –grep app to run one or the other. The same goes for any other part of a suite or test-case title, –grep users would be valid as well, or even –grep GET.

describe(‘api’, function() {
describe(‘GET /api/users’, function() {
it(‘respond with an array of users’, function() {
// …
});
});
});

describe(‘app’, function() {
describe(‘GET /users’, function() {
it(‘respond with an array of users’, function() {
// …
});
});
});

Questions