stub-azure-function-context
stub-azure-function-context copied to clipboard
V4 Functions support
Did anyone get a chance to try this with the new Azure Functions version?
https://learn.microsoft.com/en-gb/azure/azure-functions/functions-node-upgrade-v4?tabs=azure-cli-set-indexing-flag%2Cv4 https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=javascript%2Clinux%2Cazure-cli&pivots=nodejs-model-v4
No - but it looks like this library may not be needed in v4 given it has a way to create contexts for tests:
I had a play with it, but I wasn't very successful. Also, there's such poor documentation, that it's hard to figure it out
I haven't spent any time at all looking into v4 for our projects, and I don't think I'm likely to soon as it looks like a rather significant refactor would be required to start supporting it. Hopefully the docs will improve and it'll be a simpler process...
Any updates on v4 please?
There are no plans to add v4 support at the moment
I upgraded my projects to @azure/functions V4. It was straightforward, but a bit tedious.
I made 3 changes to my tests:
- Changed functionRunner wrapper from
const executeHttpTrigger = async (
requestBody: unknown,
): Promise<HttpResponseSimple> =>
(await functionRunner(
functionToTest,
__dirname + "/../function.json",
{
req: new HttpBinding({
method: "POST",
body: requestBody,
}),
},
(context: Context) => {
setNullLogger(context);
},
)) as Promise<HttpResponseSimple>;
to
const executeHttpTrigger = async (
body: Record<string, unknown>,
logHandler: (...args: any[]) => void = jest.fn(),
) =>
functionToTest(
new HttpRequest({
method: "POST",
url: "https://localhost/api/article",
body: {
string: JSON.stringify(body),
},
}),
new InvocationContext({ logHandler }),
);
- Updated
expect(result)
assertions to matchjsonBody
attribute instead ofbody
(because I updated my functions to returnjsonBody
). - Updated logging tests to check
logHandler
instead of global console methods.
expect(jest.mocked(console.error)).toBeCalledWith("HTTP Error: ...");
to
expect(logHandler).toBeCalledWith("error", "HTTP Error: ...");
Also I wasted 4 hours trying to understand why do request.json()
and request.text()
getters cause OOM in some tests.
I discovered that undici library uses queueMicrotask
function which was mocked by jest.useFakeTimers()
in some tests. doNotFake: ['queueMicrotask']
fixed it.
Update: Also it is important to move @azure/functions
from devDependencies
to dependencies
in package.json, otherwise function app doesn't work on Azure.
@dhensby @willmorgan Thank you for this library - without it I would have no tests to migrate.