I have a project with expressjs and ava, and I'm using webpack in order bundle the app
webpack is also load environment variables from .env files based on the NODE_ENV.
How can I load .env file variables while running ava, or alternatively bundle the app before running ava tests?
Thanks!
You can use dotenv to load .env
file into your test environment. A simple example:
import test from 'ava';
require('dotenv').config()
test('foo', t => {
console.log(process.env.DB_HOST);
console.log(process.env.DB_USER);
console.log(process.env.DB_PASS);
t.pass();
});
This commit was merged 1st June of 2019, quite a while after the original post.
The long and short of it now, though, is that you can define environment vars for the Ava test runner inside of your project's package.json
file:
{
...
"ava": {
"environmentVariables": {
"FOO": "bar",
...
}
},
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With