@applitools/eyes-storybook
Version:
153 lines (132 loc) • 5.19 kB
JavaScript
;
const lodash = require('lodash');
const utils = require('@applitools/utils');
const {resolve} = require('path');
const {deprecationWarning, InvalidConfigFileError} = require('./errMessages');
const uniq = require('./uniq');
const {DEFAULT_CONCURRENCY} = require('@applitools/core');
const MAX_DATA_GAP = DEFAULT_CONCURRENCY * 2;
function generateConfig({argv = {}, defaultConfig = {}, externalConfigParams = []}) {
const defaultConfigParams = Object.keys(defaultConfig);
const configPaths = argv.conf ? [resolve(process.cwd(), argv.conf)] : undefined;
const configParams = uniq(defaultConfigParams.concat(externalConfigParams));
const config = getAndParseConfig({configPaths, configParams});
const argvConfig = lodash.pick(argv, configParams);
const result = Object.assign({}, defaultConfig, config, argvConfig);
// backward compatibility
if (result.waitBeforeCapture === defaultConfig.waitBeforeCapture) {
if (result.waitBeforeScreenshots !== defaultConfig.waitBeforeScreenshots) {
console.log(
deprecationWarning({
deprecatedThing: "'waitBeforeScreenshots'",
newThing: "'waitBeforeCapture'",
}),
);
result.waitBeforeCapture = result.waitBeforeScreenshots;
}
if (result.waitBeforeScreenshot !== defaultConfig.waitBeforeScreenshot) {
console.log(
deprecationWarning({
deprecatedThing: "'waitBeforeScreenshot'",
newThing: "'waitBeforeCapture'",
}),
);
result.waitBeforeCapture = result.waitBeforeScreenshot;
}
}
if (typeof result.waitBeforeCapture === 'string' && !isNaN(parseInt(result.waitBeforeCapture))) {
result.waitBeforeCapture = Number(result.waitBeforeCapture);
}
if (result.showLogs === '1' || process.env.APPLITOOLS_SHOW_LOGS === 'true') {
result.showLogs = true;
}
result.testConcurrency = utils.types.isInteger(result.testConcurrency)
? result.testConcurrency
: utils.types.isInteger(result.concurrency)
? result.concurrency * 5
: undefined; // let the server handle this
result.eyesServerUrl = result.serverUrl;
// Auto-enable dontCloseBatches when sharding is used (unless explicitly set by user)
if (result.shard && result.dontCloseBatches === undefined) {
result.dontCloseBatches = true;
console.log(
'Auto-enabling dontCloseBatches due to sharding configuration - please make sure to close batches manually ( https://applitools.com/tutorials/concepts/best-practices/batching#manually-close-the-shared-batch ).',
);
}
result.keepBatchOpen = result.dontCloseBatches;
if (result.batchName) {
result.batch = {name: result.batchName, ...result.batch};
}
if (result.batchId) {
result.batch = {id: result.batchId, ...result.batch};
}
if (result.storyDataGap === undefined) {
// determine storyDataGap based on default concurrency or user configured concurrency
// the actual concurrency will be determined by the server
const concurrency = result.testConcurrency || DEFAULT_CONCURRENCY;
result.storyDataGap = Math.max(Math.min(concurrency * 2, MAX_DATA_GAP), concurrency);
}
transformConfig(result);
if (!result.environments) {
result.environments = [{name: 'chrome', width: 1024, height: 768}];
}
return result;
}
function getAndParseConfig({configPaths, configParams}) {
try {
return utils.config.getConfig({
paths: configPaths,
params: configParams,
strict: true,
traverse: false,
});
} catch (error) {
if (error.message.includes('Could not find configuration file')) {
return utils.config.populateConfigParams({config: {}, params: configParams});
}
throw new InvalidConfigFileError(error);
}
}
function transformConfig(result) {
transformLayoutBreakpoints(result);
transformBrowser(result);
}
function transformBrowser(result) {
if (result.browsersInfo) {
transformBrowsersInfo(result.browsersInfo);
delete result.browsersInfo;
}
if (result.browser) {
transformBrowsersInfo(result.browser);
delete result.browser;
}
return result;
function transformBrowsersInfo(info) {
result.environments = [];
if (!utils.types.isArray(info)) {
info = [info];
}
result.environments = info.map(browser => {
if (browser.deviceName) {
return {chromeEmulationInfo: browser};
} else if (utils.types.has(browser, 'iosDeviceInfo')) {
const {iosVersion, ...iosDeviceInfo} = browser.iosDeviceInfo;
return {iosDeviceInfo: {...iosDeviceInfo, version: iosVersion ?? iosDeviceInfo.version}};
} else if (!browser.name) {
return {...browser, name: 'chrome'};
}
return browser;
});
}
}
function transformLayoutBreakpoints(result) {
if (
utils.types.isBoolean(result.layoutBreakpoints) ||
utils.types.isArray(result.layoutBreakpoints)
) {
// Normalize shorthand forms to canonical object shape
result.layoutBreakpoints = {breakpoints: result.layoutBreakpoints};
}
// Object forms ({breakpoints, reload?, heightBreakpoints?}) are already canonical and pass through as-is
}
module.exports = {generateConfig, transformConfig};