willmorgan / stub-azure-function-context Goto Github PK
View Code? Open in Web Editor NEWCreate a stub context object for use in testing your Function Apps
License: BSD 3-Clause "New" or "Revised" License
Create a stub context object for use in testing your Function Apps
License: BSD 3-Clause "New" or "Revised" License
Hi,
consider having a function that makes a call to an external database such as Cosmos DB. In the business logic I plan to have an abstract database class for which I can build my own in-memory implementation for testing.
When I run a test the function needs to know if it should use CosmosDB as backend or the in-memory implementation for testing.
My idea was to evaluate "context.state.testing" for this, however I don't seem to be able to set this.
Currently the functionRunner
has a return type of Promise<any>
.
This results in a few typescript-eslint
errors:
Trying to type with Promise<Context>
:
For now we've got around this by adding an overload to our project:
import * as stubAzureFunctionContext from 'stub-azure-function-context';
declare module 'stub-azure-function-context' {
export function functionRunner(
azFunction: AzureFunction,
bindingDefinitions?: stubAzureFunctionContext.BindingDefinition[] | string,
bindingData?: Record<string, stubAzureFunctionContext.Binding>,
augmentContext?: stubAzureFunctionContext.AugmentContextCallback,
): Promise<Context>;
}
It'd be ideal if the type were narrowed in the package itself. Is there a reason it's currently any
? Is Context
a sufficient type?
Struggling to work out how to pass routing parameters, any chance of a documentation update to show this?
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
First, appreciate the time you guys spent to craft this. Very thorough implementation of Azure protocol.
I'm probably missing something but specifying binding definitions in the test file seems redundant to me. Since I'm testing some function I really want binding definitions to be the same as in the function.json
because that's how Azure runtime will invoke my function.
The question is: what's the valid scenario where I may want to override binding definitions configured in function.json
? I'm thinking about adding a little helper which would parse and load relevant bits from the config file, so that a user only needs to provide a trigger object.
As I see it, this library currently only supports a limited set of bindings listed at https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings?tabs=csharp#supported-bindings. Are there any plans of including more, or is there some documentation you guys could provide so I myself can look into how feasible it is to create a PR?
I'm specially interested in the IoT Hub
binding ...
{
"bindings": [
{
"type": "httpTrigger",
"name": "req",
"direction": "in",
"methods": [ "get" ],
"route": "products/{category:alpha}/{id:int?}"
},
{
"type": "http",
"name": "res",
"direction": "out"
}
]
}
module.exports = async function (context, req) {
var category = context.bindingData.category;
var id = context.bindingData.id;
var message = `Category: ${category}, ID: ${id}`;
context.res = {
body: message;
}
}
I've got simular code as in the link above but using functionRunner
context.bindingData.id;
remains undefined. is this supported in the lib ? It was working in the prevous version ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.