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?
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:
--exit, --watch), which are only available on the CLI--reporter, --ui), which are available on the CLI and in the Mocha object (used in browser and in the programmatic API)--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:
--dash-separated value formsetup function (as in the linked documentation)run (e.g. mocha.timeout(2000))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));
Most helpful comment