Can we read environmental variables in karma tests? If yes, can somebody let me know . process.env is not recognized in karma test.
No. Since the tests are running in the browser, process.env is not
available.You can however set the client args. See the documentation at http://karma-runner.github.io/0.13/config/configuration-file.html (search for client.args).
On Thu, Apr 21, 2016, 01:54 s-sridevi15 [email protected] wrote:
Can we read environmental variables in karma tests? If yes, can somebody
let me know . process.env is not recognized in karma test.—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/karma-runner/karma/issues/2066
Thanks budde377 for quick reply. So, there is no feature to read environmental variables in karma tests.
When I was researching, I got this information about karma-env-preprocessor(https://www.npmjs.com/package/karma-env-preprocessor), when I tried plugin and run tests(gulp karma -Ddevice_url) through command line by passing environmental variables with -D option, it is not taking reading the variables to window.env . Could you let me know whether karma-env-preprocessor plugin works to read environmental variables in karma tests?
If yes, am I passing environmental variables wrong?
If passing and reading environmental variables not possible in karma test, I wiil look for some other work around . Thanks in advance and will appreciate if you can clarify my question
@s-sridevi15, I don't know about that specific plugin, but as I see it, it should expose the environment variables as the window.__env__ variable, not the window.env variable as you described. Furthermore I don't know about the -D options (outside of Java). Normally I would just set the environment with the good old export command.
I any case, Karma doesn't support exposing environment variables natively, so I would suggest that you contact the karma-env-preprocessor project if your can't seem to get it to work. Hopefully they can provide better support than I.
Thanks a lot budde377 for clarification. I am trying other work arounds and will try that.
Any progress with the karma-env-preprocessor plugin ?
Hi folks, if anyone is simply trying to use environment variables defined in a .env file in your karma.config.js file, this plugin will do the trick: https://www.npmjs.com/package/dotenv
You just install the module, then add require('dotenv').config(); to the top of the karma.config.js file and all the environment variables in .env will be appended to process.env.
For example (obviously truncated config for demonstration):
require('dotenv').config();
module.exports = function (config) {
config.set({
proxies: {
'/api': 'http://' + process.env.APP_URL + '/api'
}
});
};
As stated by @camslice works with dotenv. Ex:
'/dist/': 'http://localhost:' + process.env.PORT + '/',
dotenv might be more than necessary. You can call karma like this to pass env vars into your karma.config.js:
MY_ENV_VAR="blahblah" karma start ./config/karma.config.js
If you need the env vars to be inside of your tests, then one option is to use karma-webpack, which allows you to process test code using Webpack, and Webpack allows you to define global variables that are placed into your code at build time (before karma runs the tests Webpack will compile them).
Even if you don't need to compile anything, you can use karma-webpack just for this purpose. In your karma.config.js file, you can configure webpack like this:
const webpack = require('webpack')
module.exports = {
webpack: { // this is standard Webpack configuration here (read Webpack docs), use any loaders you want here, or specify env vars:
// ...
// For example, pass globals from your CLI command to your test code:
plugins: [
new webpack.DefinePlugin({
MY_ENV_VAR: process.end.MY_ENV_VAR
})
]
}
}
Now any place in your test code that has MY_ENV_VAR will be replaced with the value that you passed into your MY_ENV_VAR="......." karma start command.
Most helpful comment
Hi folks, if anyone is simply trying to use environment variables defined in a
.envfile in yourkarma.config.jsfile, this plugin will do the trick: https://www.npmjs.com/package/dotenvYou just install the module, then add
require('dotenv').config();to the top of thekarma.config.jsfile and all the environment variables in.envwill be appended toprocess.env.For example (obviously truncated config for demonstration):