Skip to content

armgit / mocha

Public Imported from GitHub
0

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

1.5 KB
Raw Blame
import { Suite } from "../suite.js";
import { Test } from "../test.js";

/**
 * Exports-style (as Node.js module) interface:
 *
 *     exports.Array = {
 *       '#indexOf()': {
 *         'should return -1 when the value is not present': function() {
 *
 *         },
 *
 *         'should return the correct index when the value is present': function() {
 *
 *         }
 *       }
 *     };
 *
 * @param {Suite} suite Root suite.
 */
function exportsInterface(suite) {
  var suites = [suite];

  suite.on(Suite.constants.EVENT_FILE_REQUIRE, visit);

  function visit(obj, file) {
    var suite;
    for (var key in obj) {
      if (typeof obj[key] === "function") {
        var fn = obj[key];
        switch (key) {
          case "before":
            suites[0].beforeAll(fn);
            break;
          case "after":
            suites[0].afterAll(fn);
            break;
          case "beforeEach":
            suites[0].beforeEach(fn);
            break;
          case "afterEach":
            suites[0].afterEach(fn);
            break;
          default:
            var test = new Test(key, fn);
            test.file = file;
            suites[0].addTest(test);
        }
      } else {
        suite = Suite.create(suites[0], key);
        suite.file = file;
        suites.unshift(suite);
        visit(obj[key], file);
        suites.shift();
      }
    }
  }
}

// Required for `--list-interfaces`
exportsInterface.description = 'Node.js module ("exports") style';

export { exportsInterface };