Comments (9)
@evanshortiss My unit tests require files that require the config.js
file, which forces me to have the env vars set in order for tests to pass.
Unit tests don't depend on any environment variable (e.g. AUTH_TOKEN), but I still want to use env-var
❤️
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 ...
from env-var.
const SECRET = env.get('SECRET', 'bad-secret').required(NODE_ENV === 'production').asString()
Very smart.
from env-var.
@gunar it sounds like you'd like to be able to have the required()
function behave differently based on a specific condition - in your case that condition is the value NODE_ENV?
Would you mind providing a code example of how you would like to see this feature work?
from env-var.
I understand now. I don't think NODE_ENV=test
is standard so I would be very cautious adding it to env-var
since people will not expect that behaviour, right?
I have some thoughts:
- Use stubs or mocks in your tests. This is probably the best choice in general.
- Modify the
config.js
to change behaviour in test mode. - Add a new feature to
env-var
. - Use dependency injection - I list this since it's an option, but might need you to restructure modules
Option 1
Using mocks in your unit tests will allow you to stub/mock out config.js
entirely. This is a good approach since you can adjust it for each test case if necessary. I'm not sure what text framework you use, but Jest supports class mocks and has a host of other mocking features. If not using jest
, then maybe use proxyquire
with mocha
or another test runner?
Option 2
You 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 3
Add a feature like this in env-var
where required
accepts an optional parameter function to customise the behaviour.
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()
from env-var.
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)
from env-var.
@gunar let me know your thoughts
from env-var.
@gunar closing this, but if you have any further input feel free to reopen.
from env-var.
@gunar, check out the new "required()" function behavior. It can support this now.
from env-var.
Thanks @evanshortiss !
from env-var.
Related Issues (20)
- Feature request: asRegExp() HOT 4
- Add 'example/README.md' HOT 4
- Add description() function HOT 3
- The number accessors are too permissive HOT 3
- asEnum should accept readonly T[] HOT 3
- add asMailString() HOT 4
- Configure default behavior / Support dotenv-defaults HOT 5
- Duplicate identifier causing broken build with typescript. HOT 3
- Duplicate identifier 'EnvVarError'. HOT 4
- Deno Support HOT 3
- TypeError: env.get is not a function HOT 3
- Don't default export `from(process.env)` for environments that don't have `process` HOT 6
- v8.0.0 HOT 1
- Custom validation extension/custom accessor before `as` check. HOT 2
- Client side. HOT 6
- Required() in React HOT 1
- Allow asUrlString (and maybe other checks) to be optional HOT 2
- 7.4.0 is breaking create-react-app projects that worked in prior versions HOT 11
- When testing, env variables can't be change from the test HOT 3
- Bug with `default` and `asEnum` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from env-var.