Running testcase with jest, I get a WARNING: Failed to detect the Azure Functions runtime. Switching "@azure/functions" package to test mode - not all features are supported.
. As jest does not load the local.settings.json
file, its exposed to process.env
by func start
, its possible its missing an env variable, or it might be that its not detecting the underlaying func
runtime (which as far as I know cant run during testing)?
What is the intended pattern when testing azure functions? As of now my testcases do run but I cannot suppress this error as its caught and printed to console.
Showing the simplest test here just to give an idea on how the azure function library reacts:
// src/functions/httpHealth.ts
import '#src/utils/appinnsights';
import { HttpRequest, InvocationContext, app } from '@azure/functions';
import { StatusCodes } from 'http-status-codes';
export const handler = async (
request: HttpRequest,
context: InvocationContext,
) => {
return { status: StatusCodes.OK };
};
app.http('health', {
methods: ['GET', 'POST'],
handler,
});
// test/functions/health.test.ts
import { handler } from '#src/functions/httpHealth';
import { HttpRequest, InvocationContext } from '@azure/functions';
import { StatusCodes } from 'http-status-codes';
test('health', async () => {
const context = new InvocationContext({ functionName: 'health' });
const request = new HttpRequest({
url: 'http://localhost/health',
method: 'POST',
});
const expected = { status: StatusCodes.OK };
expect(await handler(request, context)).toStrictEqual(expected);
});
Edit:
I have to clarify as it might not be obvious from the code if you haven't worked with it, I am using the new v4 of @azure/functions
. It has support for making mock contexts for testing, it should not require running the functions core tools in the background. It runs just fine right now, but it throws and catches an error presumedly due to a lack of configuration and sets itself into test mode. I would like to configure this preemptly to suppress the warning.
WARNING: Failed to detect the Azure Functions runtime. Switching"@azure/functions" package to test mode - not all features are supported.
Make sure you have Azure Functions Core tools installed in your system and added in the environment variable path of your system before running the Azure Functions locally.
If you already have Azure Function core tools installed, You can delete it from this path for Windows -C\Program Files\Microsoft\Azure Functions Core Tools and install it again from this Link.
In order to run your function as host, Use the command below:-
func host start
func host start --javascript #with language
I cloned a sample Azure Function Javascript Jest testing repository by lakshaykaushik and the tests were successful, Refer below:-
Commands:-
npm run-script test
npx jest --verbose
Output:-
I am using npx command to run the jest tests.
If your Jest file is unable to load local.settings.json, You can create one setup.js file and load that file with dotenv module like below:-
const dotenv = require('dotenv');
dotenv.config({ path: 'path to local.settings.json' });
process.env.MySetting = process.env.MySetting || 'default value';
I also created one sample HTTP function to test with Jest and the Function Triggered successfully without any runtime specific errors, Refer below:-
Make sure you have your Azure Function core tools runtime running like below, You can run your function either by clicking fn + f5 or Run > start debugging or by this command func host start
Make sure you have the necessary dependencies installed like below:-
npm install --save-dev jest @azure/functions
As a reference go through the Links below:-
How to test Azure HTTP Trigger Function in Jest and TypeScript? - Stack Overflow
Unit testing Azure Functions with Jest and TypeScript (maxivanov.io)