-
Notifications
You must be signed in to change notification settings - Fork 39
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
Feature: required() if process.env.NODE_ENV != 'test' #23
Comments
@gunar it sounds like you'd like to be able to have the Would you mind providing a code example of how you would like to see this feature work? |
@evanshortiss My unit tests require files that require the Unit tests don't depend on any environment variable (e.g. AUTH_TOKEN), but I still want to use Code example// src/config.js
import * as env from 'env-var'
export const AUTH_TOKEN = env.get('AUTH_TOKEN').required().asString() // src/businessLogic.js
import * from src/config
// ... business logic code ... // test/businessLogic.spec.js
import * from src/businessLogic
// ... tests ... |
I understand now. I don't think I have some thoughts:
Option 1Using mocks in your unit tests will allow you to stub/mock out Option 2You could use fake values if in test mode: import * as env from 'env-var'
const isTest = env.get('NODE_ENV').asString() === 'test'
const AUTH_TOKEN = isTest ? 'FAKE_VALUE' : env.get('AUTH_TOKEN').required().asString() Option 3Add a feature like this in import * as env from 'env-var'
const isRequired = () => env.get('NODE_ENV').asString() !== 'test'
// required only throw if NODE_ENV is != "test"
export const AUTH_TOKEN = env.get('AUTH_TOKEN').required(isRequired).asString() |
option 4// src/businessLogic
// We do not require config.js here, instead we have it passed by the callee
export default function (config) {
// use config.AUTH_TOKEN
} Then in the test: // test/businessLogic.spec.js
import businessLogic from src/businessLogic
const bl = bl({ AUTH_TOKEN: 'fake token for testing' }) Or in real code: import businessLogic from src/businessLogic
import * from src/config as config
const bl = bl(config) |
@gunar let me know your thoughts |
@gunar closing this, but if you have any further input feel free to reopen. |
@gunar, check out the new "required()" function behavior. It can support this now. |
Thanks @evanshortiss ! |
const SECRET = env.get('SECRET', 'bad-secret').required(NODE_ENV === 'production').asString() Very smart. |
Would you be willing to implement it? I can send a PR too if you need me to
The text was updated successfully, but these errors were encountered: