cypress: Unable to use deeply nested config or env in plugins

Current behavior:

When returning deeply nested config in the plugin, an error is shown on startup. “Unable to set property {propertyname} of undefined.”

Desired behavior:

When returning deeply nested config in the plugin, the config should be merged into the main config, and no error is shown.

Steps to reproduce:

Simply place the following in your plugins/index.js file.

module.exports = () => ({
  foo: {
    bar: 'baz',
  },
});

Versions

Cypress version 2.1.0

Other info

I did a little bit of digging and it tries to merge this extra config into “resolved” config but as cfg.resolved.foo doesn’t exist, it fails.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 11
  • Comments: 16 (5 by maintainers)

Most upvoted comments

Yeah, I was trying to use separate config files per environment following the Config API docs here: https://docs.cypress.io/api/plugins/configuration-api.html#Promises

Nested values in my Cypress.json work fine, but when following the above pattern I’m also getting the same error:

image

Eg. We’re trying to use values like this:

// cypress/config/dev.json
{
    "baseUrl": "https://dev-website.com",
    "env":
    {
        "api": "https://api.dev-website.com",
        "users":
        {
            "admin":
            {
                "username": "admin",
                "password": "adminpassword"
            },
            "super":
            {
                "username": "super",
                "password": "superpassword"
            },
        }
    }
}

In our app we needed to lean on node-config to deal with per-environment configurations. We ended up doing something like:

module.exports = (on, config) => {
    config.env.CONFIG = JSON.stringify(require('config'));
}

in plugins/index.js and something like:

    const config = JSON.parse(Cypress.env('CONFIG'));

in the tests. This approach seems to work fine for deeply nested structures.

This issue, along with #1859 makes it a lot more flexible to start Cypress through [ts-]node and pass env vars to it, rather than doing it “the Cypress way” with plugins.

I feel like I’m missing something. Any reason not to do it?

Is there a plan to allow custom configuration properties?

@mike-rogers’ solution does work, but it seems very hacky.