UNPKG

kocha

Version:

Modern, simpler Mocha clone, no globals, lint friendly

147 lines (122 loc) 3.88 kB
'use strict'; var path = require('path'); var existsSync = require('fs').existsSync; var ms = require('ms'); var kocha = require('../'); var lookupFilesAll = require('../utils/lookup-files-all'); var lookupFiles = lookupFilesAll.lookupFiles; var color = require('../utils/color' /** * Runs the tests. * @param {Object} argv The command line options parsed by minimist */ );module.exports = function (argv) { if (argv._.length === 0) { noInputFilesAndExit(); } // --require processRequireOption(argv.require // --config );processConfigOption(argv.config // --timeout );processTimeoutOption(argv.timeout); var files = lookupFilesAll(argv._, { cwd: process.cwd() }); if (files.length === 0) { noInputFilesAndExit(); } files.forEach(function (file) { require(file); }); var Reporter = require('../reporters/spec'); new Reporter(kocha.getRunner()); // eslint-disable-line no-new kocha.run().then(function (allPassed) { process.exit(allPassed ? 0 : 1); }).catch(function (e) { console.log(e); }); }; /** * Processes --require option * @param {string|string[]|undefined} requireOption The require option */ var processRequireOption = function processRequireOption(requireOption) { var modules = [].concat(requireOption).filter(Boolean); modules.forEach(function (moduleName) { processSingleRequireOption(moduleName); }); }; /** * Processes --config option * @param {?string} config The config path */ var processConfigOption = function processConfigOption(config) { if (config) { if (!existsSync(config)) { showErrorAndExit('The given config file is not found: ' + config); } requireModuleAndShowMessage(path.resolve(process.cwd(), config)); } else if (existsSync('kocha.config.js')) { // Loads default kocha.config.js if exists requireModuleAndShowMessage(path.resolve(process.cwd(), 'kocha.config.js')); } }; /** * Processes --timeout option * @param {?string} timeout The timeout duration */ var processTimeoutOption = function processTimeoutOption(timeout) { if (timeout == null) { return; } var duration = ms(timeout); if (duration == null) { showErrorAndExit('The timeout duration is invalid: "' + timeout + '"'); } console.log('Setting timeout duration: ' + color('cyan', duration + 'ms')); kocha.timeout(duration); }; /** * Shows error message for no input files and exits. */ var noInputFilesAndExit = function noInputFilesAndExit() { console.log(color('error message', 'Error: ') + 'No input file'); console.log('See ' + color('cyan', 'kocha -h') + ' for the usage'); process.exit(1); }; /** * Processes the signle require option path. * @param {string} modulePath The module path */ var processSingleRequireOption = function processSingleRequireOption(modulePath) { try { var resolvedPath = require.resolve(modulePath); requireModuleAndShowMessage(resolvedPath); } catch (e) { var modules = lookupFiles(modulePath, { cwd: process.cwd() }); if (modules.length > 0) { modules.forEach(function (module) { var resolvedPath = path.resolve(module); requireModuleAndShowMessage(resolvedPath); }); } else { console.log(color('fail', 'Error: module not found, ' + modulePath)); process.exit(1); } } }; /** * Requires the module of the resolved path and shows the message. * @param {string} resolvedPath The resolved path name */ var requireModuleAndShowMessage = function requireModuleAndShowMessage(resolvedPath) { console.log(color('magenta', 'Requiring: ') + resolvedPath); require(resolvedPath); }; /** * Shows the error message and exits. * @param {string} message The error message */ var showErrorAndExit = function showErrorAndExit(message) { console.log(color('error message', 'Error: ') + message); process.exit(1); };