Skip to content
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

Is it possible to enable changing a variable at runtime? #60

Open
bcanseco opened this Issue Mar 27, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@bcanseco
Copy link

bcanseco commented Mar 27, 2019

This could be pretty useful for unit tests:

// source.js
import {NODE_ENV} from '@env';

export const doSomethingBasedOnEnvironment = () => {
  if (NODE_ENV === 'production') {
    // business logic here
    return true;
  } else {
    // business logic here
    return false;
  }
};
// source.spec.js

import {doSomethingBasedOnEnvironment} from '.';
import * as env from '@env';

it('should return true in production', () => {
  env.NODE_ENV = 'production';
  expect(doSomethingBasedOnEnvironment()).toBeTruthy();
});

it('should return false in development', () => {
  env.NODE_ENV = 'development';
  expect(doSomethingBasedOnEnvironment()).toBeFalsy();
});

Maybe a contrived example, but in real-world applications the current alternatives aren't ideal:

  • Edit the API only for the tests' sake:
    // source.js
    import {NODE_ENV} from '@env';
    
    export const doSomethingBasedOnEnvironment = (environment = NODE_ENV) => {
      if (environment === 'production') {
        return true;
      } else {
        return false;
      }
    };
  • Fall back to exclusively using process.env due to the problem below:
    import {NODE_ENV} from '@env';
    
    process.env.NODE_ENV = 'something-else';
    expect(NODE_ENV).toEqual('something-else'); // throws
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.