DEV Community

Markus Huggler for Novaloop Ltd

Posted on • Edited on

Complex Configuration in Cypress.io

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);
};
Enter fullscreen mode Exit fullscreen mode

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>'))

We have configured our Cypress.io Test Suite with Gitlab CI/CD. The pipeline can be triggered manually for the production and for the staging environemnt and is run automatically once a day.

Need more details?

Please let us know if you are interested in more details!

Top comments (0)