Easily invoke your Functions from test harnesses, etc.
This is a fork from https://github.com/christopheranderson/azure-functions-node-harness. I am using this repo for testing a few ideas and prototyping. Please leave feedback (through github issues) if you love it, hate it, or have suggestions.
coming soon to npm.
for now: npm install jsturtevant/azure-functions-node-harness --save-dev
var func = require('azure-functions-node-harness');
var queueFunc = func('queue'); // Optional config: , {dirname: __dirname});
var invocation = queueFunc.invoke({'hello':'world'});
invocation.then(function(context) {
// validate result
if (context.bindings.out == "somevalue"){
console.log("success");
}
})
var func = require('azure-functions-node-harness');
var queueFunc = func('queue'); // Optional config: , {dirname: __dirname});
var httpFunc = new func('queue'); //same thing - supports new and factory idioms
- nameOrPath: string
- Selects the name or path you want to load. Uses node module loading logic (i.e.
queue
will look for./queue/index.js
, )
- Selects the name or path you want to load. Uses node module loading logic (i.e.
- config: Object
- Helps adjust settings. Mostly advanced features.
- Properties:
- dirname: string
- Which directory to look in for functions. Useful when tests are in a different directory than sample functions.
- moduleConfig: object
- instead of looking up function you can pass a function and it's function.json manually. mostly used internally.
- dirname: string
var func = require('azure-functions-node-harness');
var queueFunc = func('queue');
// Supports callbacks
queueFunc.invoke({parameterName: {'hello':'world'}}, function(err, results) {
// add results handling logic here
};
// Returns a promise if no callback is given
var invocation = queueFunc.invoke({parameterName: {'hello':'world'}});
invocation.then(function(context){
// success logic here
}).catch(function(err){
// failure logic here
});
- data
- Key-value list of inputs. Use the name of your bindings for the keys
- cb
- Optionally give a callback for your Function. If you don't, the function will return a Promise.
Invoke http trigger functions. It is possible to use the invoke
to get the same results but this simplifies the building of the request object.
var httpFunction = func('httpfunc');
httpFunction.invokeHttpTrigger({
reqBody: requestBody,
method: "POST", //optional
headers: headers //optional, along with any other request parameters you might want to tweak
}, {parameterName: "another parameter"}).then(context => {
// do test validations here.
});
If you use the sample.dat
file for testing locally with the Azure functions CLI you can use that file by calling invokeHttpTrigger
with no parameters.
var httpFunction = func('httpfunc');
// sample.dat is used for request body.
httpFunction.invokeHttpTrigger().then(context => {
// do test validations here.
});
- httpTrigger
- object with reqBody. Simplifies building the entire http request object. Can override any parameters like
headers
.
- object with reqBody. Simplifies building the entire http request object. Can override any parameters like
- data
- Key-value list of other inputs. Use the name of your bindings for the keys
- cb
- Optionally give a callback for your Function. If you don't, the function will return a Promise.
The general idea is to set up the function once then call invoke in each test. If you use chai and chai-as-promised, you should be able to have some nifty assertions.
coming soon
coming soon
coming soon
Your test file would look like:
var test = require('tape');
var funcHarness = require('azure-functions-node-harness');
test('Tests', function (group) {
var funcToTest = funcHarness('NameOfFunction', { dirname: 'foldername-functions-live-in' });
group.test('test to run', function (t) {
t.plan(1);
funcToTest.invoke({
data: {}
}).then(context => {
t.equal("yippee!", context.binding.output);
}).catch(err =>{
t.fail(`something went wrong during test: ${err}`);
});
});
Clone this repository then run:
npm install
npm test