package.cypress-configs.plugins.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
// @ts-nocheck
///
/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
const path = require("path");
const webpack = require("@cypress/webpack-preprocessor");
const {
cypressBrowserPermissionsPlugin,
} = require("cypress-browser-permissions");
const { cloudPlugin } = require("cypress-cloud/plugin");
const fs = require("fs-extra");
const getConfigurationByFile = file => {
const pathToConfigFile = `cypress/config/cypress.${file}.json`;
return file && fs.readJsonSync(path.join(process.cwd(), pathToConfigFile));
};
const globalState = "globalState.txt";
const merge = (target, source = {}) => {
Object.keys(source).forEach(key => {
if (source[key] && typeof source[key] === "object") {
merge((target[key] = target[key] || {}), source[key]);
return;
}
target[key] = source[key];
});
return target;
};
module.exports = (on, config) => {
const environment = config.env.configFile;
const configForEnvironment = getConfigurationByFile(environment);
const options = {
// send in the options from your webpack.config.js
webpackOptions: require("./webpack.config"),
watchOptions: {},
};
on("file:preprocessor", webpack(options));
on("task", {
log(...message) {
//eslint-disable-next-line
console.log(...message);
return null;
},
});
config = cypressBrowserPermissionsPlugin(on, config);
const newEnvironment = merge(config, configForEnvironment);
require("@cypress/grep/src/plugin")(newEnvironment);
require("@cypress/code-coverage/task")(on, newEnvironment);
cloudPlugin(on, newEnvironment);
on("before:browser:launch", (browser, launchOptions) => {
if (["chrome", "edge"].includes(browser.name) && browser.isHeadless) {
launchOptions.args.push("--no-sandbox");
launchOptions.args.push("--disable-gl-drawing-for-tests");
launchOptions.args.push("--js-flags=--max-old-space-size=5500");
launchOptions.args.push("--disable-gpu");
}
return launchOptions;
});
const readFileSyncIfExists = filePath => {
try {
return JSON.parse(fs.readFileSync(filePath, "utf8"));
} catch (error) {
if (error.code === "ENOENT") {
return {};
}
console.log(error); // eslint-disable-line
}
return {};
};
const writeDataToFile = (filePath, data) => {
try {
fs.writeFileSync(filePath, data, "utf8");
} catch (err) {
console.log(err); // eslint-disable-line
}
return true;
};
on("task", {
getGlobalState: key =>
key
? readFileSyncIfExists(globalState)?.[key] ?? null
: readFileSyncIfExists(globalState),
updateGlobalState: ({ key, value }) => {
const data = readFileSyncIfExists(globalState);
data[key] = value;
return writeDataToFile(globalState, JSON.stringify(data));
},
bulkUpdateGlobalState: newState =>
writeDataToFile(globalState, JSON.stringify(newState)),
});
return config, newEnvironment;
};