Dotenv: How do I run testing without creating .env.test?

Created on 2 Apr 2017  路  5Comments  路  Source: motdotla/dotenv

Hi,
How do I run testing without creating .env.test?
when I run the test (that inside test folder), it uses the .env file in the main folder.
but I want to run the test with different environment variables.
image

Most helpful comment

Try to work your tests toward smaller chunks - where your test is only dependent on an ENV or two at a time. Mocking really helps with this and mock-env will do the trick for you.

If you really want to include a .env.test you can do so by specifying the path to it, and then call dotenv.config(path: '') inside a before suite hook of your test suite: https://github.com/motdotla/dotenv#path

All 5 comments

Hi @shai32, great question.

Ideally, you should mock or set them individually in each test. Here is a nice tool to mock them:

https://www.npmjs.com/package/mock-env

and here is a good example of setting them:

http://stackoverflow.com/questions/24589021/how-to-stub-process-env-in-node-js

mock-env look redundant.
the stackoverflow suggestion is to just change it directly (and return the original value to env after)
but this also is not a good idea. because I have a lot of tests files that all need the same env variables
so I need to write down those variables for each file.

adding .env.test solve all this issues.

Try to work your tests toward smaller chunks - where your test is only dependent on an ENV or two at a time. Mocking really helps with this and mock-env will do the trick for you.

If you really want to include a .env.test you can do so by specifying the path to it, and then call dotenv.config(path: '') inside a before suite hook of your test suite: https://github.com/motdotla/dotenv#path

@mostlyjason yea I used the path options, works great.

Hi @motdotla and @shai32

I'm working on Unit Test of angular 2. I use Dotenv for defining application variables for Dev and Prod env.
Now I want to use for my test. Could you show me the way in details ?
Thanks
Nam

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mattcdavis1 picture mattcdavis1  路  5Comments

datasmurfen picture datasmurfen  路  5Comments

shellscape picture shellscape  路  3Comments

samayo picture samayo  路  3Comments

tikotzky picture tikotzky  路  4Comments