I have Database values in .env file.
It works fine.
I wrote a test.js for unit testing. It throws error.
The reason was, during test run .env variables where not accessed.
How to make .env variables available for test execution. How to resolve it?
Hi @vishnu2prasadh
Can you share some information about how you're trying to load your .env variables.
For reference, this type of problem was recently answered here as well:
https://github.com/motdotla/dotenv/issues/127
Hi @markowsiak,
Thanks for your reference. But still it is not working any other suggestion?
From your reference
I tried the following,
var assert = require('chai').assert
var path = require('path');
var dotEnvPath = path.resolve('./.env');
require('dotenv').config({ path: dotEnvPath})
var config = require('../lib/services/config.js')
// In config.js i will be using 'process.env.DB_PORT'
console.log(process.env.DB_PORT, config)
// This printed process.env.DB_PORT value correctly.
//But the variables in config(in config.js I have used the same process.env.DB_PORT) were undefined
var serviceToBeTested = require('../lib/services/myservice.js');
describe("Test Suite", function() {
it("Test Case", function(done) {
this.timeout(15000);
serviceToBeTested.functionToBeTested('valueToBeTested').then(function(res){
console.log(res);
done();
});
});
});
Require dotenv like you did here in the config.js file
Great @lmj0011 worked well. Thanks a lot.
@markowsiak & @lmj0011 Thanks for handling this one!
@vishnu2prasadh It sounds like this is resolved, but let us know if you run into any further problems with it and we can always reopen the issue.
Most helpful comment
Hi @vishnu2prasadh
Can you share some information about how you're trying to load your .env variables.
For reference, this type of problem was recently answered here as well:
https://github.com/motdotla/dotenv/issues/127