首页 > 解决方案 > 在 Azure 函数(Nodejs)中使用 Jest 进行测试时未定义 process.env 变量

问题描述

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.jsazurejestjsazure-functions

解决方案


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


推荐阅读