Mocha: Very hard to find docs for using mocha programmatically

Created on 9 Dec 2017  路  4Comments  路  Source: mochajs/mocha

mochajs.org and the github wiki do not have any obvious links to the mocha API for using mocha in a script.

The best I could quickly find was the wiki article "Using mocha programmatically", which only gives a short example.

Is there any docs that list the methods of the mocha object?

Most helpful comment

import * as Mocha from 'mocha';

const mocha = new Mocha();
mocha.timeout(timeout);

const testFiles = glob.sync('**/*.spec.js', {cwd: directory});
testFiles.forEach(file => mocha.addFile(path.join(directory, file)));


const failures = await new Promise((resolve) => mocha.run(resolve));

All 4 comments

there is an issue that was opened yesterday addressing this https://github.com/mochajs/mocha/issues/3138

Going to close this as a duplicate of #3138, but to answer the immediate question --

Is there any docs that list the methods of the mocha object?

It's not explicitly listed in one place, but the API is largely the same as in the browser; more generally, Mocha's options can be thought of as falling into a few groups:

  • options that affect the process as a whole (e.g. --exit, --watch), which are only available on the CLI
  • options that can only be set for the entire test run (e.g. --reporter, --ui), which are available on the CLI and in the Mocha object (used in browser and in the programmatic API)
  • options that can be set for the entire test run or for particular suites and tests (e.g. --timeout), which are available on the CLI, in the Mocha object and on this in suites and tests

...Then, there are a few different ways they can be set:

  • on the CLI with --dash-separated value form
  • on the Mocha object by passing a configuration object to the constructor or setup function (as in the linked documentation)
  • on the Mocha object by calling a getter/setter method before calling run (e.g. mocha.timeout(2000))
  • on suites'/tests' this by calling a getter/setter method (e.g. this.timeout(2000))

...And whereas the CLI options are --dash-separated, the options in the configuration object and the getter/setter methods are camelCased.

(I'm not sure that's 100% bulletproof, but it covers most of the cases as far as I recall -- basically, it's less a matter of every last combination needing to be learned or memorized, and more a matter of learning which options are applicable on which levels plus how each place formats the same option.)

@ScottFreeCode Thanks for the tips.
From what you're telling me, programmatically I should be using the CLI in a shell script or use child-process.exec to run mocha's cli with the parameters that I want.

import * as Mocha from 'mocha';

const mocha = new Mocha();
mocha.timeout(timeout);

const testFiles = glob.sync('**/*.spec.js', {cwd: directory});
testFiles.forEach(file => mocha.addFile(path.join(directory, file)));


const failures = await new Promise((resolve) => mocha.run(resolve));
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Aarbel picture Aarbel  路  3Comments

robertherber picture robertherber  路  3Comments

3p3r picture 3p3r  路  3Comments

jamietre picture jamietre  路  3Comments

CADBOT picture CADBOT  路  3Comments