I'm working to add tests to a pre-existing monorepo that uses TypeScript in the main. All other tests are in TypeScript and for mine, I wish to use Cypress.
I need to run them on two separate environments and need to understand why my environment variables are not being picked up.
It's probably something very obvious but I am a TS and JS newcomer so hoping for some gentle guidance.
Directly under /config I have dev.json and prod.json for the two envs:
{
"env": {
"ENV": "dev",
"BASE_URL": "http://example.com",
}
}
My plugins/index.ts as follows:
const fs = require('fs-extra');
const path = require('path');
export default (on: Cypress.PluginEvents, _config: Cypress.PluginConfigOptions): void => {
on('task', {
log(message) {
console.log(message)
return null
}
});
on('task', {
table(message) {
console.table(message);
return null;
},
});
/**
* @type {Cypress.PluginConfig}
*/
function getConfigurationByFile(file) {
const pathToConfigFile = path.resolve('config', `${file}.json`
);
return fs.readJson(pathToConfigFile);
}
/**
* @type {Cypress.PluginConfig}
*/
module.exports = (on, config) => {
const file = config.env.configFile || 'dev';
return getConfigurationByFile(file);
};
}
Example of use within a test spec:
beforeEach(()=> {
cy.login(Cypress.env('BASE_URL'),
})
Example script in package.json
"test:headless": "npm cypress run --env 'configFile=%ENV%",
Running from CLI as: ENV=dev npm run test:headless
This fails to pick up the url from the dev.json
Again, I'm sure it's something dumb but some pointers would be appreciated.
Try the CYPRESS_ prefix Option #3: CYPRESS_*
Any OS-level environment variable on your machine that starts with either CYPRESS_ or cypress_ will automatically be added to Cypress' environment variables and made available to you.
CYPRESS_configFile=dev npm run test:headless
...
const file = config.env.configFile || 'dev';