Giter Club home page Giter Club logo

cypress-extends's Introduction

@bahmutov/cypress-extends

renovate-app badge ci status badges status cypress version

Cypress plugin that adds "extends" support to the configuration file. This allows the config files to remain DRY and avoid duplicating data, but still have multiple files for different scenarios. See details in Extending the Cypress Config File blog post.

Use

npm i -D @bahmutov/cypress-extends
# or
yarn add -D @bahmutov/cypress-extends

Add this module to your cypress/plugins/index.js file

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  return require('@bahmutov/cypress-extends')(config.configFile)
}

Start using extends syntax in your Cypress configuration file

// cypress.json
{
  "extends": "./base.json"
}
// base.json
{
  "fixturesFolder": false,
  "supportFile": false,
  "$schema": "https://on.cypress.io/cypress.schema.json"
}

Tip: the $schema property in JSON files lets modern IDE show intelligent code completion for Cypress config values.

Schema

Debugging

Run Cypress with environment variable

DEBUG=@bahmutov/cypress-extends npx cypress run
# or
DEBUG=@bahmutov/cypress-extends npx cypress open

Common problems

baseUrl is not set Make sure you do not accidentally place the baseUrl or another config variable into the env block. The following is incorrect and won't work:
{
  "env": {
    "baseUrl": "http://localhost:3030",
    "FOO": "bar"
  }
}

Instead place the baseUrl at the top level, outside the env object.

{
  "baseUrl": "http://localhost:3030",
  "env": {
    "FOO": "bar"
  }
}

More info

Read Cypress tips and tricks and watch Cypress tips videos.

Small print

Author: Gleb Bahmutov <[email protected]> © 2020

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2020 Gleb Bahmutov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

cypress-extends's People

Contributors

bahmutov avatar mnf avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cypress-extends's Issues

Variables from custom config files are not visible in test runners Settings > Configuration

When using this plugin as described, after opening test runner Settings tab > Configuration section, then I am able to see that config will be marked for properties that are set from custom config file qa.json, but on the other hand property baseUrl is null and not displaying the value from that file nor marking it in blue as filled from config (instead it says it is default).
Note that while running the test I can see that it is targeting baseUrl defined in qa.json. It is just not visible here in test runner.
I am not sure if this is cypress issue rather then this plugin issue.

image

Can't override array values

Setup is really simple. The cypress.json contains the following config value:
{ "langArray": ["en", "de"] }
and another json file, e.g. custom.json, which extends the cypress.json looks like this:
{ "extends": "./cypress.json", "langArray": ["es", "fr"] }

When calling cypress.io with the custom.json config, we would expect "langArray" only contains the values "es" and "fr". However, the arrays were merged and the result contains all four values.
"langArray": ["en", "de", "es", "fr"]

Is this behaviour intended? If not, it would be great to have this changed.

Unable to Set pluginsFile Path In Base Json File

When I attempt to place the the pluginsFile key/value in the base json file, Cypress looks to the environment specific json for this value, and since it's not there, assumes the default location.

I also noticed values under env required in the plugins file aren't found if placed in the base json file. Would be nice if there was a way to support all base values being loaded from the base file.

cypress.json(base json file)

{
    "pluginsFile": "../../plugins/index.js",
    "testFiles": "**/*.feature",
    "nodeVersion": "system",
    "env": {
        "db": {
            "port": 5444
        }
    }
}

pluginsFile and db.port not recognized

Additionally, node values set into the browser context, although visible in the config returned from plugins/index.js via console.log(), are not present in the browser, and are not found(used cy.log() to confirm).

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Order of precedence is "broken"

Unfortunately, this breaks the order of precedence for how configurations are defined. Here's what Cypress shows:

Screen Shot 2020-08-20 at 2 49 43 PM

Normally (without this plugin), I could define a config JSON file but then override those values with either env vars or passing arguments on the command line. But now, the extended config JSON files are read in by the plugin, which has the highest precedence.

Not sure if there is a way to fix this, other than having the plugin construct the merged config from JSON files, then override with env vars and CLI args. It would all still show up as coming from a plugin, but at least it would be working as expected.

Environment Variables are lost when merging config files

When using the plugin my environment variables are lost, I think because they aren't part of the actual config file, but are added to the config, they aren't included in the merged config file (which is purely the two config files, not anything else).

To get this to work for me, I wrote my own function, based on the plugin, that looks like this:

const mergeConfig = (config, filename) => {
  const configJson = require(filename);
  if (configJson.extends) {
    let baseConfigFilename;
    if (configJson.extends.startsWith('.')) {
      baseConfigFilename = path.join(path.dirname(filename), configJson.extends);
    } else {
      baseConfigFilename = require.resolve(configJson.extends);
    }
    const baseConfig = require(baseConfigFilename);
    return deepmerge.all([config, baseConfig, configJson]);
    // eslint-disable-next-line no-else-return
  } else {
    return configJson;
  }
};

So it merges the config, the baseConfig, and the config file rather than just merging the two json files - this appears to work for me.

Happy to try and make a PR to update the plugin if this looks like something that would work for all cases/I'm not missing anything?

Cannot extend from packages in node_modules

Our project keeps all config in a separate package that individual repos import/extend from. External packages located in the node_modules directory are not resolved properly when trying to use the cypress-extends plugin.

cypress.json

    ...
    extends: "@some-lib/cypress-config"
    ....

potential solution

Extending configuration file with Cypress 10

More of a question rather than a bug.
With changes in Cypress 10 (switching from .json to .js or .ts configs) could you please explain/update on how do we extend configs now?

Unable to set the projectId in a base config

I realize this isn't an issue with this plugin per se, but it is unexpected in what I would think is a common use case.

I'm running Cypress in CI, and I'm using the --record flag.
I started using this plugin, and I moved the projectId to my base config.
However, Cypress then failed. Apparently, if --record is used, it has to find the projectId in the specified config file, or it bails.

Relevant output is
You passed the --record flag but this project has not been setup to record. This project is missing the 'projectId' inside of 'cypress/config/cypress.vrt.dev.json'. We cannot uniquely identify this project without this id. You need to setup this project to record. This will generate a unique 'projectId'. Alternatively if you omit the --record flag this project will run without recording.

Expected behavior:
Cypress can run with the --record flag with a projectId set in a base config file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.