af/envalid

testDefault?

novemberborn opened this issue · 7 comments

devDefault - A fallback value to use only when NODE_ENV is not 'production'. This is handy for env vars that are required for production environments, but optional for development and testing.

I'm using envalid in a server setup which needs to interact with AWS resources. I have a use case where a variable is required during development, but not during testing. I could set devDefault but that'll just lead to runtime errors. It'd be great if envalid could support a testDefault which only applies when NODE_ENV === 'test'.

Interesting. Currently I manually pass args when calling envalid.cleanEnv. Is that not feasible?

I've written a testOnly() method:

function testOnly (testDefault) {
  return process.env.NODE_ENV === 'test'
     ? testDefault
     : undefined
}

Which lets me do:

APP_COGNITO_CLIENT_ID: validators.cognitoUserPoolClientId({devDefault: testOnly('stub+client+id')}),

Which is an OK workaround, but having a testDefault property would be tidier.

af commented

I like your workaround, and can't really think of a better one to be honest.

The issue I see with adding testDefault is that devDefault currently applies to both NODE_ENV=development and NODE_ENV=test, so IMO it'd be confusing if devDefault and testDefault overlapped. Also I'm not sure if test-only env vars are common enough to justify adding the feature– personally devDefault has always been sufficient for me.

Maybe we could add your testOnly helper function to the lib as a compromise?

Fair enough.

You mean require('envalid').testOnly(value)?

af commented

Yep, that's exactly what I'm thinking

Cool, will keep this issue in mind. Have some other projects also hoping for my attention 😄

If anybody else wants to pick this up, feel free!

How to use different values for test and for production?
I have an .env file created and the value is always from it