stub-azure-function-context icon indicating copy to clipboard operation
stub-azure-function-context copied to clipboard

V4 Functions support

Open w0rldart opened this issue 1 year ago • 6 comments

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

w0rldart avatar Jul 07 '23 10:07 w0rldart

No - but it looks like this library may not be needed in v4 given it has a way to create contexts for tests:

dhensby avatar Jul 10 '23 08:07 dhensby

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

w0rldart avatar Jul 10 '23 10:07 w0rldart

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

dhensby avatar Jul 10 '23 13:07 dhensby

Any updates on v4 please?

molinto avatar Oct 24 '23 14:10 molinto

There are no plans to add v4 support at the moment

dhensby avatar Oct 24 '23 14:10 dhensby

I upgraded my projects to @azure/functions V4. It was straightforward, but a bit tedious.

I made 3 changes to my tests:

  1. 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 }),
  );
  1. Updated expect(result) assertions to match jsonBody attribute instead of body (because I updated my functions to return jsonBody).
  2. 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.

Naktibalda avatar Nov 21 '23 12:11 Naktibalda