我正在用摩卡咖啡进行测试。我有以下配置文件:
{
"diff": true,
"extension": [
"js"
],
"package": "./package.json",
"reporter": "spec",
"slow": 75,
"timeout": 2000,
"ui": "bdd",
"recursive": true,
"exit": true,
"require": "esm, dotenv/config"
}
由于我所有的代码都使用import语句,因此我正在使用'esm'软件包来使其正常工作。我也在使用“ dotenv”包加载我的.env文件。
我只是分割了.env文件用于测试,开发和生产。但是,我无法获得用于加载正确的.env文件的摩卡咖啡。在dotenv文档中,是一个命令行参数(dotenv_config_path
),可用于指定.env文件:
$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars
我尝试将其添加到配置以及命令行中,但没有成功。
命令行:
mocha -r dotenv/config -r esm --recursive --exit dotenv_config_path=./env/dev-config.env
但出现以下错误:
Error: No test files found: "dotenv_config_path=./env/dev-config.env"
但是,如果根目录中存在.env
文件,此命令将起作用:
mocha -r dotenv/config -r esm --recursive --exit
那么如何获取自定义的.env
文件来加载?
想通了。将此添加到摩卡选项文件:
"env" : "dotenv_config_path=path/to/env/file"