Hello I am trying to write UT for following function :
function checkEnvirenmentHost() {
var hostDetails
if (process.env.HTTPS_HOST) {
hostDetails = process.env.HTTPS_HOST;
} else if (process.env.https_HOST) {
hostDetails = process.env.https_HOST;
} else if (process.env.HTTP_HOST) {
hostDetails = process.env.HTTP_HOST;
} else if (process.env.http_HOST) {
hostDetails = process.env.http_HOST;
} else {
hostDetails = false;
}
return hostDetails
}
the issue is if I run UT in different environment the UT will fail because the environment variable might not be present, so is there a way to upload a custom .env file while running Unit test in jasmine. I read about this feature is available in 'jtest' but I cant find the same for jasmine. Can someone pls suggest a way ?
CodePudding user response:
There is no need to load environment variables from .env
file. You can set the environment variable in each test case to test each code branch.
E.g.("jasmine": "^3.6.3")
index.test.js
:
const checkEnvirenmentHost = require('./');
describe('72563579', () => {
it('should pass - HTTPS_HOST', () => {
process.env.HTTPS_HOST = '127.0.0.1';
const actual = checkEnvirenmentHost();
expect(actual).toEqual('127.0.0.1');
process.env.HTTPS_HOST = undefined;
});
it('should pass - https_HOST', () => {
process.env.https_HOST = '127.0.0.1';
const actual = checkEnvirenmentHost();
expect(actual).toEqual('127.0.0.1');
process.env.https_HOST = undefined;
});
});
Test result:
Executing 2 defined specs...
Running in random order... (seed: 37424)
Test Suites & Specs:
1. 72563579
✔ should pass - https_HOST (5ms)
✔ should pass - HTTPS_HOST (1ms)
2 specs, 0 failures
Finished in 0.022 seconds
Randomized with seed 37424 (jasmine --random=true --seed=37424)
>> Done!
Summary: