Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment Variables with Ava

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!

like image 742
Eyal Cohen Avatar asked Dec 23 '22 13:12

Eyal Cohen


2 Answers

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();
});
like image 177
ufxmeng Avatar answered Jan 02 '23 16:01

ufxmeng


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",
            ...
        }
    },
    ...
}
like image 40
Cameron Hurd Avatar answered Jan 02 '23 14:01

Cameron Hurd