Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Very hard to find docs for using mocha programmatically #3140

Closed
BebeSparkelSparkel opened this issue Dec 9, 2017 · 4 comments
Closed

Very hard to find docs for using mocha programmatically #3140

BebeSparkelSparkel opened this issue Dec 9, 2017 · 4 comments

Comments

@BebeSparkelSparkel
Copy link

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?

@akrawchyk
Copy link
Contributor

there is an issue that was opened yesterday addressing this #3138

@ScottFreeCode
Copy link
Contributor

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.)

@BebeSparkelSparkel
Copy link
Author

@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.

@gatsbyz
Copy link

gatsbyz commented Feb 8, 2019

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));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants