UNPKG

stryker-mocha-runner

Version:

A plugin to use the mocha test runner in Stryker, the JavaScript mutation testing framework

110 lines 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var path = require("path"); var fs = require("fs"); var MochaRunnerOptions_1 = require("./MochaRunnerOptions"); var plugin_1 = require("stryker-api/plugin"); var MochaOptionsLoader = /** @class */ (function () { function MochaOptionsLoader(log) { this.log = log; this.DEFAULT_MOCHA_OPTS = 'test/mocha.opts'; } MochaOptionsLoader.prototype.load = function (config) { var mochaOptions = Object.assign({}, config[MochaRunnerOptions_1.mochaOptionsKey]); return Object.assign(this.loadMochaOptsFile(mochaOptions.opts), mochaOptions); }; MochaOptionsLoader.prototype.loadMochaOptsFile = function (opts) { switch (typeof opts) { case 'boolean': this.log.debug('Not reading additional mochaOpts from a file'); return {}; case 'undefined': var defaultMochaOptsFileName = path.resolve(this.DEFAULT_MOCHA_OPTS); if (fs.existsSync(defaultMochaOptsFileName)) { return this.readMochaOptsFile(defaultMochaOptsFileName); } else { this.log.debug('No mocha opts file found, not loading additional mocha options (%s.opts was not defined).', MochaRunnerOptions_1.mochaOptionsKey); return {}; } case 'string': var optsFileName = path.resolve(opts); if (fs.existsSync(optsFileName)) { return this.readMochaOptsFile(optsFileName); } else { this.log.error("Could not load opts from \"" + optsFileName + "\". Please make sure opts file exists."); return {}; } } }; MochaOptionsLoader.prototype.readMochaOptsFile = function (optsFileName) { this.log.info("Loading mochaOpts from \"" + optsFileName + "\""); return this.parseOptsFile(fs.readFileSync(optsFileName, 'utf8')); }; MochaOptionsLoader.prototype.parseOptsFile = function (optsFileContent) { var _this = this; var options = optsFileContent.split('\n').map(function (val) { return val.trim(); }); var mochaRunnerOptions = Object.create(null); options.forEach(function (option) { var _a; var args = option.split(' ').filter(Boolean); if (args[0]) { switch (args[0]) { case '--require': case '-r': args.shift(); if (!mochaRunnerOptions.require) { mochaRunnerOptions.require = []; } (_a = mochaRunnerOptions.require).push.apply(_a, args); break; case '--timeout': case '-t': mochaRunnerOptions.timeout = _this.parseNextInt(args); break; case '--async-only': case '-A': mochaRunnerOptions.asyncOnly = true; break; case '--ui': case '-u': mochaRunnerOptions.ui = _this.parseNextString(args); break; case '--grep': case '-g': var arg = "" + _this.parseNextString(args); if (arg.startsWith('/') && arg.endsWith('/')) { arg = arg.substring(1, arg.length - 1); } mochaRunnerOptions.grep = new RegExp(arg); break; default: _this.log.debug("Ignoring option \"" + args[0] + "\" as it is not supported."); break; } } }); return mochaRunnerOptions; }; MochaOptionsLoader.prototype.parseNextInt = function (args) { if (args.length > 1) { return parseInt(args[1], 10); } else { return undefined; } }; MochaOptionsLoader.prototype.parseNextString = function (args) { if (args.length > 1) { return args[1]; } else { return undefined; } }; MochaOptionsLoader.inject = plugin_1.tokens(plugin_1.commonTokens.logger); return MochaOptionsLoader; }()); exports.default = MochaOptionsLoader; //# sourceMappingURL=MochaOptionsLoader.js.map