-
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
Dotenv module doesn't work with cypress #1358
Comments
You cannot require npm modules which use native node libs like If you want to read in config files and then set env vars into Cypress you can use the Configuration Plugin API to do just that. Files you write in the |
Thanks. That makes perfect sense. But if I do what you suggest then types don't work from this module: add-typescript-to-cypress |
There's a plugin that claims to put env vars from dotenv files are extremely popular (8,000,000+ downloads / week for dotenv on npm). It would be really nice if Cypress supported them. |
for someone who will be looking for working examples: // cypress/plugins/index.js
module.exports = (on, config) => {
const configWithDotenv = require('dotenv').config();
if (configWithDotenv.error) {
throw configWithDotenv.error;
}
const env = { ...config.env, ...configWithDotenv.parsed };
const result = { ...config, env };
return result;
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
|
@alex-popov-tech Can't seem to get this to work? My |
I have been trying like a mad man to figure out how to do this with my NextJS project. Want to inject my environment variables into
This only gives me access to |
@Norfeldt I think the example is for reading process.env variables and setting them as Cypress.env so you can read them in Cypress test files. You won't be able to read |
@jennifer-shehane it took me some time to understand this https://stackoverflow.com/questions/67700529/programmatically-declare-typescript-types-for-environment-keys-in-my-env In order for me to not care about if it is running in node (with template:
export default function env(key: TEnvKey): string {
try {
// have to try catch because of Cypress reference error
return Cypress.env(key);
} catch {
switch (key) {
// START OF AUTO GENERATED CASES -- DO NOT REMOVE OR EDIT THIS COMMENT
// this part will be replaced
// END OF AUTO GENERATED CASES -- DO NOT REMOVE OR EDIT THIS COMMENT
default:
return undefined;
}
}
}
// START OF AUTO GENERATED TYPES -- DO NOT REMOVE OR EDIT THIS COMMENT
// this will be replaced
import { config } from 'dotenv'
import { writeFileSync, readFileSync } from 'fs'
const envLocal = config({ path: '.env.local' }).parsed // NOTE: Script has to be executed from the project root
const typeText = `type TEnvKey =\n | "${Object.keys(envLocal).join('"\n | "')}"`
const casesText = Object.keys(envLocal).reduce((acc, key) => {
acc += ` case '${key}':\n`
acc += ` return process.env.${key}\n`
return acc
}, '')
const filePath = 'utils/env.ts'
let text = readFileSync(filePath, { encoding: 'utf8' })
console.log(`reading ${filePath}`)
if (!text) throw 'nothing read'
const casesStartLineMarker =
'// START OF AUTO GENERATED CASES -- DO NOT REMOVE OR EDIT THIS COMMENT\n'
const casesEndLineMarker =
' // END OF AUTO GENERATED CASES -- DO NOT REMOVE OR EDIT THIS COMMENT\n'
text = `${
text.split(casesStartLineMarker)[0]
}${casesStartLineMarker}${casesText}${casesEndLineMarker}${
text.split(casesStartLineMarker)[1].split(casesEndLineMarker)[1]
}`
const typesLineMarker = '// START OF AUTO GENERATED TYPES -- DO NOT REMOVE OR EDIT THIS COMMENT\n'
const [untouchedCode, _types] = text.split(typesLineMarker)
const updatedText = `${untouchedCode}${typesLineMarker}${typeText}`
writeFileSync(filePath, updatedText, 'utf8') so now I can just do |
@alex-popov-tech I am using export async function setupNodeEvents(on, config) {
// This is required for the preprocessor to be able to generate JSON reports after each run, and more,
await preprocessor.addCucumberPreprocessorPlugin(on, config);
on("file:preprocessor", browserify.default(config));
// Make sure to return the config object as it might have been modified by the plugin.
return config;
} |
Bug
Current behavior:
https://www.npmjs.com/package/dotenv
In order to use this module to have to require('dotenv').
If I do this anywhere in code it fails:
The text was updated successfully, but these errors were encountered: