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
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
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
});
});
Most helpful comment
or if you don't want to manually specify the keys, just use: