Azure/azure-functions-nodejs-library

Typescript function app tests using supertest

Closed this issue ยท 4 comments

I was trying to figure way to generate a test to hit a function app using jest and supertest libraries, any hint on how this could be performed? I didn't found any documentation from it.
The only solution i found is to test the handler directly not being able to test the actual "mount" of the function.

If you want the actual mount, you'd have to run Azure Functions Core Tools in a child process. We do this for our own end-to-end tests here if you want a quick example

Thanks, @ejizba I ended up in writting unit tests and call the function app handler, the only thing I wasn't able to test is the handler registration, for some reason I wasn't being able to mock the app.get.

handler.ts

app.get("function-app", {
  authLevel: "function",
  handler: Handler,
});

Any ideas on how this could be tested without having the need to mount core tools? Also noticed an annoying error while running tests.

  console.warn
    WARNING: Skipping call to register function "function-app" because the "@azure/functions" package is in test mode.

      79 | }
      80 |
    > 81 | app.get("function-app", {
         |     ^
      82 |   authLevel: "function",
      83 |   handler: Handler,
      84 | });

      at generic (node_modules/@azure/functions/dist/webpack:/@azure/functions/src/app.ts:143:17)
      at http (node_modules/@azure/functions/dist/webpack:/@azure/functions/src/app.ts:92:5)
      at Object.get (node_modules/@azure/functions/dist/webpack:/@azure/functions/src/app.ts:71:5)
      at Object.<anonymous> (src/functions/Handler.ts:81:5)
      at Object.<anonymous> (src/functions/Handler.test.ts:1:1)

Unfortunately the app.get functionality relies on core tools, which is also why you're getting that warning. There's no real way to workaround that

Thanks for the feedback, I will close this.
I have to say that v4 version interface is closer from node/express experience, the thing I miss most is indeed techniques and documentation to use the tools we often use while developing on node express ecosystem.