【问题标题】:process.env variable undefined on test using Jest in Azure function(Nodejs)在 Azure 函数(Nodejs)中使用 Jest 进行测试时未定义 process.env 变量
【发布时间】:2023-04-02 22:07:02
【问题描述】:

process.env 变量在运行yarn run jest 时显示未定义。我已将每个配置变量存储在我的 local.settings.json 文件中,并且它在正常调试中运行良好。

我的代码是这样的:

local.settings.json:

{
    "IsEncrypted": false,
    "Host":  {
                 "LocalHttpPort": 7071,
                 "CORS": "*"
             },
    "Values":  {
                   "IOTHUB_CONNECTION_STRING":  "xxxxxxxxxxxxxxxxxx",
                   "AZURE_DEVOPS_URL":  "xxxxxxxxxxxxxxxxxxxx"
               }
}

jest.config.js:

module.exports = {
  log: jest.fn(),
}

天蓝色 httptrigger 函数 - index.js:

const iothubConnectionString = process.env["IOTHUB_CONNECTION_STRING"];
console.log( iothubConnectionString ) //undefined

由于这些配置变量,我的测试失败了。我需要通过考试。谁能帮帮我。

【问题讨论】:

  • 请不要使用图片来分享您的代码。编辑您的问题并将代码直接包含在您的问题中。

标签: node.js azure jestjs azure-functions


【解决方案1】:

local.settings.json 特定于 Azure Functions 核心工具。只要你使用 Core Tools CLI 运行函数,你就可以通过 process.env 访问该文件中定义的环境变量

【讨论】:

  • 感谢您的回复 Pragna,如果我对 process.env 变量值进行硬编码,我的测试将通过。那为什么如果我使用 process.env 作为变量,测试失败了?
猜你喜欢
  • 2019-09-11
  • 2021-03-11
  • 2018-06-10
  • 2019-10-26
  • 2019-02-03
  • 1970-01-01
  • 2019-11-26
  • 2019-01-16
  • 2017-07-20
相关资源
最近更新 更多