process.env 的测试中缺少所有自定义变量
All custom defined variables mssing in test from process.env
dotenv 8.2
安装在我的 nodejs 12.15
应用程序中。 .env
文件位于 app_root/config/
下,应用的 index.js
文件中需要 dotenv
:
require("dotenv").config(path: __dirname+"/config/.env");
应用程序在应用程序的根子目录中运行良好 "node index.js"。但是,当在应用程序根 npm test
下使用 jest 25.0
进行测试时,数据库连接会抛出错误,因为它调用 process.env.DB_PASSWORD
作为数据库密码,而 returns 什么也没有。当 console.log process.env
在测试环境中时,我注意到自定义定义的变量完全丢失了。如何恢复测试环境中丢失的所有自定义变量?
似乎当 Jest 运行ning 时,它不包括您的 index.js
,其中需要 dotenv
个变量。
运行 与 .env 变量开玩笑的选项很少:
// npm run script
{
"scripts": {
"test": "jest --setupFiles dotenv/config"
}
}
// jest.config.js
module.exports = {
setupFiles: ["dotenv/config"],
}
// jest in package.json
{
"name": "my-package",
"jest": {
"setupFiles": ["dotenv/config"]
}
}
以下也适用。在应用程序的根目录中创建一个jest.config.js:
require('dotenv').config({path: process.cwd() +'/config/.env'});
module.exports = {
verbose:true,
testTimeout:10000
}
并在package.json中添加脚本:
"test": "cross-env NODE_ENV=test jest --config=./jest.config.js --watchAll"
dotenv 8.2
安装在我的 nodejs 12.15
应用程序中。 .env
文件位于 app_root/config/
下,应用的 index.js
文件中需要 dotenv
:
require("dotenv").config(path: __dirname+"/config/.env");
应用程序在应用程序的根子目录中运行良好 "node index.js"。但是,当在应用程序根 npm test
下使用 jest 25.0
进行测试时,数据库连接会抛出错误,因为它调用 process.env.DB_PASSWORD
作为数据库密码,而 returns 什么也没有。当 console.log process.env
在测试环境中时,我注意到自定义定义的变量完全丢失了。如何恢复测试环境中丢失的所有自定义变量?
似乎当 Jest 运行ning 时,它不包括您的 index.js
,其中需要 dotenv
个变量。
运行 与 .env 变量开玩笑的选项很少:
// npm run script
{
"scripts": {
"test": "jest --setupFiles dotenv/config"
}
}
// jest.config.js
module.exports = {
setupFiles: ["dotenv/config"],
}
// jest in package.json
{
"name": "my-package",
"jest": {
"setupFiles": ["dotenv/config"]
}
}
以下也适用。在应用程序的根目录中创建一个jest.config.js:
require('dotenv').config({path: process.cwd() +'/config/.env'});
module.exports = {
verbose:true,
testTimeout:10000
}
并在package.json中添加脚本:
"test": "cross-env NODE_ENV=test jest --config=./jest.config.js --watchAll"