question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 issue

Test 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
sindresorhuscommented, Nov 10, 2016

@novemberborn process is requirable:

require('process');
1reaction
novemberborncommented, Nov 10, 2016

Where are you setting / overwriting the variable?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found