Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

1.6 KB
Raw Blame
/**
 * Contains "command" code for "one-and-dones"--options passed
 * to Mocha which cause it to just dump some info and exit.
 * See {@link module:lib/cli/one-and-dones.ONE_AND_DONE_ARGS ONE_AND_DONE_ARGS} for more info.
 * @module
 * @private
 */

import Mocha from "../mocha.cjs";

/**
 * Dumps a sorted list of the enumerable, lower-case keys of some object
 * to `STDOUT`.
 * @param {Object} obj - Object, ostensibly having some enumerable keys
 * @ignore
 * @private
 */
const showKeys = (obj) => {
  console.log();
  const keys = Object.keys(obj);
  const maxKeyLength = keys.reduce((max, key) => Math.max(max, key.length), 0);
  keys
    .filter(
      (key) =>
        /^[a-z]/.test(key) &&
        !obj[key].browserOnly &&
        !Object.hasOwn(obj[key], "abstract"),
    )
    .sort()
    .forEach((key) => {
      const description = obj[key].description;
      console.log(
        `    ${key.padEnd(maxKeyLength + 1)}${
          description ? `- ${description}` : ""
        }`,
      );
    });
  console.log();
};

/**
 * Handlers for one-and-done options
 * @namespace
 * @private
 */
const ONE_AND_DONES = {
  /**
   * Dump list of built-in interfaces
   * @private
   */
  "list-interfaces": () => {
    showKeys(Mocha.interfaces);
  },
  /**
   * Dump list of built-in reporters
   * @private
   */
  "list-reporters": () => {
    showKeys(Mocha.reporters);
  },
};

/**
 * A Set of all one-and-done options
 * @type Set<string>
 * @private
 */
const ONE_AND_DONE_ARGS = new Set(
  ["help", "h", "version", "V"].concat(Object.keys(ONE_AND_DONES)),
);

export { ONE_AND_DONES, ONE_AND_DONE_ARGS };