-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: allow configuration file to be 'js', not 'json' #5218
Comments
Might I suggest the name |
This comment has been minimized.
This comment has been minimized.
Having a js file for configuration will be really helpful for using environment variables as well. Hope this issue will be addressed soon |
using |
I will also agree with this +1 |
+1 to this as well! it will be very easy to write and add comments! |
It is trivial to combine Mozilla Convict with a Cypress plugin to define flexible, dynamic, Cypress-aware configs such as baseUrl. cypress/plugins/index.js: module.exports = (on, cypressConfig) => {
const config = require('./config').load(cypressConfig);
return {
// override any cypress configs here:
baseUrl: config.get('baseUrl'),
// make all Convict configs available via Cypress.env():
env: config.getProperties(),
};
}; cypress/plugins/config.js: const convict = require('convict');
const json5 = require('json5');
const fs = require('fs');
const path = require('path');
convict.addParser({ extension: 'json5', parse: json5.parse });
function load(cypressConfig) {
// pick environment from cypress `--env` param or `cypress_env` environment var:
const env = cypressConfig.env.env || 'local';
// define config defaults in Convict schema:
const schema = {
baseUrl: {
format: String,
default: 'http://localhost:7480',
},
someOtherConfig: {
format: String,
default: 'bloop',
env: 'SOME_OTHER_CONFIG',
},
};
return convict(schema)
// dynamically load config values for current env:
.loadFile(path.resolve(__dirname, '..', `config-${env}.json5`))
.validate({ allowed: 'strict' });
}
module.exports = { load }; cypress/config-dev.json5:
Run Cypress:
There are some drawbacks with this approach. The Cypress visualization of Config values will show all your overridable config values coming from the "plugin" layer, regardless of how Convict picked them. Cypress will also not auto restart on changes to the custom config files. I think these are acceptable tradeoffs though. You can still define Cypress configs that do not need to change between environments in Either JSON5 or YAML are great choices for the environmental config files but any file format can be used. |
This comment has been minimized.
This comment has been minimized.
I'd love to be able to have
Jest now does support I'm happy to help implement this, and answer questions (I've done this a few times now 😅) |
Is this implemented in #18061 ? #5674 (comment) led me to #17000 (closed but not merged) which led me to #18061 (merged and comment saying it is released in cypress 8.5.0). |
It will be implemented officially in 10.0.0. You can already test version 10 by going to a commit and following the instructions in the comments. Here is more info on how to install it This being said 10.0 is still in alpha, so you might want to avoid using it for critical systems. |
This was released in 10.0. Closing as resolved. |
Goal
Simplify Cypress configuration
Why
Why is this important? Why is it a priority?
cypress.json
,cypress.env.json
,pluginsFile
) is confusing and difficult to document wellcypress.json
cypress.json
Related:
Implementation
cypress.json
andcypress.env.json
pluginsFile
to<projectRoot>/cypress.js
cypress.js
Questions
cypress.ts
,cypress.coffee
)?projectId
when a user sets up their project to recordcypress.js
to be different from the current API of thepluginsFile
. Since it's the only way to configure Cypress, it might be good to make the use-case of only using it for configuration easier. Instead of:We could allow them to export an object if they don't need to register events:
Research
How other projects deal with TypeScript/CoffeeScript for config files
.js
or.json
)Notes
via @brian-mann
Another change is that we will need to resolve default configuration and overrides prior to calling the config function so you can utilize those values.
Before it was...
now it will be...
which means we need to pass the defaults along with the overrides to the function so it can continue to change them.
I think we also need to decide what to do with
env
and whether or not this should be set as a property of the default configuration or not.We either need to keep it separate or merge it with the config.
Right now its confusing because you work with it separately in the driver with
Cypress.env(...)
but it ends up being a property onconfig
, but is set through a separate CLI flag:--env
.It should be either a separate thing or a part of config.
EDIT: we may not want to change the order of resolving the env + config else this would force the user to always merge the overrides in, else they'd be ignored. That would also force the user to have a valid
cypress.js
which we want to make optional.Disregard what I said above about setting overrides. We could go with the
defaultConfigurationOptions
since that is separate (or perhaps yield that in) but keep resolution to happen downstream.via @brian-mann
To make matters even more confusing...
CYPRESS_
env vars are special in that they...config.env
This was done a long time ago because without the ability to execute anything in
node
it was difficult to set environment variables in the traditional way and know that the user wanted them in the driver. It would be a vulnerability to just automatically attach everything onprocess.*
ontoCypress.env()
.Anyway with the ability to write configuration, setting env vars becomes much easier because you can just do the ones you care about.
If we want to allow overriding configuration we could continue to accept env vars but namespace them like
CYPRESS_CONFIG_BASE_URL=...
Setting a
CYPRESS_FOO
would no longer mean anything, but we could also still support aCYPRESS_ENV_FOO=bar
which would set{ foo: bar }
I'm still conflicted on whether or not to make
env
a property onconfig
or vice versa or keep them separate.We could namespace them like this...
Which would make more sense per how you work with them in the
driver
.But then we'd also need to namespace the events you register, which is weird...
This would potentially work better through for things like
projectId
which isn't actually part of the configuration of cypress and could live outside ofconfig
.Notes from 2/6/2018:
The text was updated successfully, but these errors were encountered: