UNPKG

testplane

Version:

Tests framework based on mocha and wdio

154 lines 6.26 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Config = exports.SelectivityMode = exports.TimeTravelMode = void 0; const path_1 = __importDefault(require("path")); const lodash_1 = __importDefault(require("lodash")); const defaults_1 = __importDefault(require("./defaults")); const browser_config_1 = require("./browser-config"); const options_1 = __importDefault(require("./options")); const logger = __importStar(require("../utils/logger")); const utils_1 = require("./utils"); var types_1 = require("./types"); Object.defineProperty(exports, "TimeTravelMode", { enumerable: true, get: function () { return types_1.TimeTravelMode; } }); Object.defineProperty(exports, "SelectivityMode", { enumerable: true, get: function () { return types_1.SelectivityMode; } }); class Config { static async create(config) { try { const { configPath, options } = await Config._resolve(config); await Config._prepareEnvironment(options); return new Config(options, configPath); } catch (e) { const error = new Error(`Got an error while trying to read config: ${e.message}`); error.stack = e.stack; error.cause = e.cause; throw error; } } static async read(configPath) { try { // eslint-disable-next-line @typescript-eslint/no-var-requires const configModule = require(path_1.default.resolve(process.cwd(), configPath)); const exported = (configModule.__esModule ? configModule.default : configModule); return await Config._resolveExportedConfig(exported); } catch (e) { logger.error(`Unable to read config from path ${configPath}`); throw e; } } static async _resolve(config) { if (typeof config === "function") { return { options: await Config._resolveExportedConfig(config) }; } if (lodash_1.default.isObjectLike(config)) { return { options: config }; } if (typeof config === "string") { return { configPath: config, options: await Config.read(config) }; } const located = Config._locateConfigPath(); if (!located) { throw new Error(`Unable to read config from paths: ${defaults_1.default.configPaths.join(", ")}`); } return { configPath: located, options: await Config.read(located) }; } static _locateConfigPath() { for (const configPath of defaults_1.default.configPaths) { try { const resolvedConfigPath = path_1.default.resolve(configPath); // eslint-disable-next-line @typescript-eslint/no-var-requires require(resolvedConfigPath); return resolvedConfigPath; } catch (err) { if (err.code !== "MODULE_NOT_FOUND") { throw err; } } } return null; } static async _resolveExportedConfig(exported) { const resolved = typeof exported === "function" ? await exported() : exported; return resolved; } static async _prepareEnvironment(options) { if (lodash_1.default.isFunction(options.prepareEnvironment)) { await options.prepareEnvironment(); } } constructor(options, configPath) { if (configPath) { this.configPath = configPath; } const parsedOptions = (0, options_1.default)({ options, env: process.env, argv: process.argv, }); (0, utils_1.addUserAgentToArgs)(parsedOptions); lodash_1.default.extend(this, parsedOptions); this.browsers = lodash_1.default.mapValues(this.browsers, (browser, id) => { const browserOptions = lodash_1.default.extend({}, browser, { id: id, system: this.system, lastFailed: this.lastFailed, }); return new browser_config_1.BrowserConfig(browserOptions); }); } forBrowser(id) { return this.browsers[id]; } getBrowserIds() { return lodash_1.default.keys(this.browsers); } serialize() { return lodash_1.default.extend({}, this, { browsers: lodash_1.default.mapValues(this.browsers, broConf => broConf.serialize()), }); } /** * This method is used in subrocesses to merge a created config * in a subrocess with a config from the main process */ mergeWith(config) { lodash_1.default.mergeWith(this, config, (l, r) => { if (lodash_1.default.isObjectLike(l)) { return; } // When passing stringified config from the master to workers // all functions are transformed to strings and all regular expressions to empty objects return typeof l === typeof r ? r : l; }); } } exports.Config = Config; //# sourceMappingURL=index.js.map