如何覆盖nodeJS中不同测试用例的环境变量? [英] how to override an environment variable for different test cases in nodeJS?

查看:33
本文介绍了如何覆盖nodeJS中不同测试用例的环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dotenv 加载 .env 文件,但我需要针对特定​​环境变量的不同值运行测试用例.但不幸的是,一旦加载 dotenv 并没有让我更改 env 变量的值,我就无法再次重置该值.

I am using dotenv to load the .env file, but I need to run the test cases for different values of a particular environment variable. But unfortunately, once loaded dotenv does not let me change the value of the env variable, I can not reset the value again.

对此有什么替代方法?

推荐答案

您可以拥有多个版本的 .env 文件,其中包含要测试的不同值.您可以通过使用 dotenv 的 parse 方法解析配置文件的备用版本来调用不同的配置.

You can have multiple versions of the .env file with the different values you want to test for. You can invoke the different configurations by parsing alternate versions of the config file using the parse method of dotenv.

例如:

var dotenv = require('dotenv');
var fs = require('fs');

var config1 = dotenv.parse(fs.readFileSync('/path/to/config1'));
var config2 = dotenv.parse(fs.readFileSync('/path/to/config1'));
var config3 = dotenv.parse(fs.readFileSync('/path/to/config1'));

这篇关于如何覆盖nodeJS中不同测试用例的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆