Environment variables (process.env.*) cannot be overwritten at test time if they are contained in a different file from the test subject
See original GitHub issueTest Source
// ./config.js
console.log('./config.js', 'process.env.GOOGLE_API_KEY', process.env.GOOGLE_API_KEY);
export const googleApiKey = process.env.GOOGLE_API_KEY;
// ./findNearbyGoogleCountries.js
import {
googleApiKey
} from './config';
export default () => {
console.log('./findNearbyGoogleCountries.js', 'process.env.GOOGLE_API_KEY', process.env.GOOGLE_API_KEY);
};
Error Message & Stack Trace
./config.js process.env.GOOGLE_API_KEY undefined
./findNearbyGoogleCountries.js process.env.GOOGLE_API_KEY foo
✖ makes a request using the GOOGLE_API_KEY false === true
Unhandled Rejection: test/services/findNearbyGoogleCountries.js
Error: Missing Google API key.
/src/findNearbyGoogleCountries.js:19:11
Generator.next (<anonymous>)
step (/src/findNearbyGoogleCountries.js:19:191)
/src/findNearbyGoogleCountries.js:19:437
/src/findNearbyGoogleCountries.js:19:99
exports.default (/src/findNearbyGoogleCountries.js:53:17)
Test.<anonymous> (findNearbyGoogleCountries.js:23:3)
Generator.next (<anonymous>)
Test.__dirname [as fn] (findNearbyGoogleCountries.js:16:1)
_combinedTickCallback (internal/process/next_tick.js:67:7)
process._tickCallback (internal/process/next_tick.js:98:9)
1 test failed [22:52:14]
1 unhandled rejection
1. makes a request using the GOOGLE_API_KEY
AssertionError: false === true
Test.<anonymous> (findNearbyGoogleCountries.js:25:3)
Generator.next (<anonymous>)
Test.__dirname [as fn] (findNearbyGoogleCountries.js:16:1)
_combinedTickCallback (internal/process/next_tick.js:67:7)
process._tickCallback (internal/process/next_tick.js:98:9)
Config
Copy the relevant section from package.json
:
{
"ava": {
"babel": "inherit",
"require": [
"babel-register"
]
}
}
Command-Line Arguments
ava --verbose
Environment
Node.js v7.0.0
darwin 16.1.0
0.16.0
3.10.8
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Why are my custom process.env not working within dotenv?
dotenv file is not loading environment variables. I'm trying to set an env file locally based on process.env.NODE_ENV . The application would be ......
Read more >Mock process.env In Your Cypress React Tests - Gleb Bahmutov
How to mock the .env values in your Cypress end-to-end tests for React applications. Let's take a React application that uses REACT_APP_..
Read more >Use environment variables in solutions - Power Apps
One environment variable can be used across many different solution components - whether they're the same type of component or different. For ...
Read more >GitLab CI/CD variables
CI/CD variables are a type of environment variable. You can use them to: Control the behavior of jobs and pipelines. Store values you...
Read more >Passing environment variables to a container
If multiple environment files are specified and they contain the same variable, they're processed in order of entry. This means that the first...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@novemberborn
process
is requirable:Where are you setting / overwriting the variable?