Ava: Environment variables (process.env.*) cannot be overwritten at test time if they are contained in a different file from the test subject

Created on 9 Nov 2016  Â·  5Comments  Â·  Source: avajs/ava

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

Most helpful comment

@novemberborn process is requirable:

require('process');

All 5 comments

Where are you setting / overwriting the variable?

Where are you setting / overwriting the variable?

Sorry, forgot an important fragment.

In test block.

import test from 'ava';
import findNearbyGoogleCountries from '../../src/services/findNearbyGoogleCountries';

test.beforeEach((assert) => {
  assert.is(process.env.GOOGLE_API_KEY, undefined);
});

@see https://github.com/avajs/ava/issues/1106

test('makes a request using the GOOGLE_API_KEY', async (assert) => {
  process.env.GOOGLE_API_KEY = 'foo';

  const scope = nock('https://maps.googleapis.com')
    .get('/maps/api/geocode/json')
    .reply(200);

  findNearbyGoogleCountries(1, 1);

  assert.true(scope.isDone());
});

Oh. This just made me realize it is not a bug in ava.

I guess the way to fix this is to use commonjs require in a test block and de-cache it.

I like to make a export default process module, then require that in order to access the process object. That way you can use something like proxyquire to stub it.

@novemberborn process is requirable:

require('process');
Was this page helpful?
0 / 5 - 0 ratings