Azure-docs: testing azure functions that require environment variables

Created on 4 Sep 2019  Â·  3Comments  Â·  Source: MicrosoftDocs/azure-docs

Hi,
I'd like to start testing my functions (majority are httptriggers, node runtime) but run into errors when functions require environment variables (set in local.settings.json) such as a storage account, a storage account key, API keys.
How can I ensure that this data is available to the function during testing using jest.
Thanks


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri1 azure-functionsvc cxp product-question triaged

Most helpful comment

or if you don't want to manually specify the keys, just use:

beforeAll(async () => {
    process.env = Object.assign(process.env, {
        ...config.Values
    });
});

All 3 comments

Hi @durtal - Thank you for your feedback! We will review and update as appropriate.

Hi @mike-urnun-msft,
I have managed to find a solution that sees all the tests run, it involves reading in the local.settings.json file and then assigning the values in there to the environment, so the top of my index.test.js file looks like below:

/**
 * @jest-environment node
 */
const httpFunction = require('./index')
const context = require('./../testing/defaultContext')
const config = require('./../local.settings.json')

beforeAll(() => {
  process.env = Object.assign(process.env, {
    AZURE_STORAGE_ACCOUNT: config.Values.AZURE_STORAGE_ACCOUNT,
    AZURE_STORAGE_ACCESS_KEY: config.Values.AZURE_STORAGE_ACCESS_KEY
  })
})

However if there's a better solution, that'd be good.
Cheers

or if you don't want to manually specify the keys, just use:

beforeAll(async () => {
    process.env = Object.assign(process.env, {
        ...config.Values
    });
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

m-andersen picture m-andersen  Â·  65Comments

renattomachado picture renattomachado  Â·  42Comments

TechTrooper picture TechTrooper  Â·  41Comments

smcd253 picture smcd253  Â·  44Comments

clangnerakq picture clangnerakq  Â·  46Comments