在 mocha 测试中指定敏感信息

Specifying sensitive information in mocha tests

我运行 mocha 测试来自下面的脚本

"test:server": "mocha test/server/  --compilers js:babel-core/register --require ./test/server/init_db.js  --recursive",

init_db 具有变量,例如到本地数据库的连接字符串。由于这是敏感的,我希望将连接字符串作为环境变量检索。

我考虑过这样设置

"test:server": " DATABASE_URL = "*****" mocha test/server/  --compilers js:babel-core/register --require ./test/server/init_db.js  --recursive"

但这并不能解决问题,因为每个人都可以看到来自 package.json 的连接字符串。对于我的开发,我使用 dotenv 并有一个 .env 文件,节点从该文件读取连接字符串并将其设置为 process.env.DATABASE_URL。但这对 mocha 不起作用,因为它无法读取指定的 .env 文件。

有没有其他方法可以设置敏感信息而无需将 init_db 放入我的 .gitignore

事实证明,我可以在我的 .env 文件中指定信息并将其导入 mocha 中,如下所示

require('dotenv').config();

现在一切正常