Skip to content

Feature - an SDK to improve testing experience #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anthonychu opened this issue Jan 19, 2022 · 2 comments
Closed

Feature - an SDK to improve testing experience #515

anthonychu opened this issue Jan 19, 2022 · 2 comments
Labels
enhancement P2 tests Make it easier for users to test their Node.js functions v4 model 🚀 Related to the new V4 programming model

Comments

@anthonychu
Copy link
Member

There are some challenges that exist today when testing Node.js Azure Functions, especially when using TypeScript. It might be useful to publish a library/SDK to help with testing.

Unit testing

When writing unit tests, we need to create mocks for context, request, response, logger, etc. This is especially difficult in TypeScript because the mocks must provide all properties/functions to satisfy the interfaces even if they're not being used.

Proposal: SDK to provide mock objects for common types used in functions.

Integration / end-to-end testing

When writing E2E tests, it would be great to be able to launch Azure Functions Core Tools and test the app by making HTTP requests, sending queue messages, etc.

Proposal: SDK to provide a Puppeteer-like API to programmatically launch and manage Core Tools:

  • Use it to start/stop an instance either before/after entire test run, each test suite, or each test
  • Ability to pass in different environment variables and settings when launching

Related: #120, #295

@ejizba ejizba added the tests Make it easier for users to test their Node.js functions label Jan 28, 2022
@ejizba ejizba added this to the Backlog Candidates milestone Jan 28, 2022
@ejizba ejizba added the P2 label Jan 28, 2022
@ejizba ejizba added the v4 model 🚀 Related to the new V4 programming model label Jun 14, 2022
@timtucker-dte
Copy link

Not sure if you'd find it helpful, but at least for logging, the solution I'd come up with was relatively simple.

instead of calling:
context.log.info("Some message")

I have a function that logs to context if available and to console.log if it isn't -- this way functions that only rely on passing in context for logging work without any issues if I just pass undefined as the context in unit tests:
function logInfo(context: Context, ...args: unknown[]) { const logger = context?.log?.info || console.log; logger.logInfo(...args); }

@ejizba
Copy link
Contributor

ejizba commented Oct 11, 2023

This was improved as a part of the new model. See here as an example. Users should be able to create a context, http request, etc. from the new sdk even in a unit test environment.

In terms of the puppeteer-like API to run e2e tests, I'm skeptical we would do that without a lot more demand from users. We have our own e2e tests and it didn't feel too difficult to manage core tools: https://github.com/Azure/azure-functions-nodejs-e2e-tests/blob/main/src/global.test.ts. Regardless, if folks still want the e2e improvements I'd prefer to track that separately form unit tests.

@ejizba ejizba closed this as completed Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement P2 tests Make it easier for users to test their Node.js functions v4 model 🚀 Related to the new V4 programming model
Projects
None yet
Development

No branches or pull requests

3 participants