Complex Configuration in Cypress.io

magbeat
magbeat
Feb 26 · 1 min read

Work in Progress

In our setup, multiple sites are deployed with the same code base. So it would make sense to reuse a lot of the testing code for all sites. To achieve this we decided to reuse the same Docker container and provide the needed configuration variables via the entrypoint script.

We have a config Folder in the cypress Folder and are building the final configuration file together from all the pieces.

To do so we use the index.js file in the pluginsfolder:

(we also installed the merge-json npm package)

const fs = require('fs-extra');
const mergeJSON = require('merge-json');
const path = require('path');


async function getConfigurationByFile(division, environment, locale) {


let defaultConfig = {
"env": {"localePrefix": locale}
}
;

const filePaths = [
path.resolve(`cypress/config/${division}`, 'defaults.json'),
path.resolve(`cypress/config/${division}`, 'pages.json'),
path.resolve(`cypress/config/${division}`, 'seo.json'),
path.resolve(`cypress/config/${division}`, `env-${environment}.json`)
]
;

for (let filePath of filePaths) {
const config = await fs.readJson(filePath);
if (config.env) {
Object.keys(config.env).forEach((key) => {
config.env[key] = JSON.stringify(config.env[key]);
});
defaultConfig = mergeJSON.merge(defaultConfig, config);
}
}

return new Promise((resolve, reject) => {
resolve(
defaultConfig
);
})
}

module.exports = (on, config) => {
const division = config.env.division;
const environment = config.env.environment;
const locale = config.env.locale;

return getConfigurationByFile(division, environment, locale);
};

Cypress does only allow to store key / value pairs env of the config. Therefore we apply JSON.stringify() on them and later on in the spec file we parse them back to JSON objects JSON.parse(Cypress.env('<env_key>'))

EcoLogic

Short and longer posts on coding with Angular and asp .net core — https://www.ecologic.ch

magbeat

Written by

magbeat

Software Engineer (.NET Core, Angular, Typescript)

EcoLogic

EcoLogic

Short and longer posts on coding with Angular and asp .net core — https://www.ecologic.ch

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade