azure-functions-node-harness

0.1.1 • Public • Published

Azure Functions Node Harness

Easily invoke your Functions from test harnesses, etc.

🚧 This library is not supported by the Azure Functions team. 🚧 Please leave feedback and suggestions (through github issues).

Install

npm i azure-functions-node-harness

Usage

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");
    }
})

FunctionsHarness(nameOrPath: string, [config: Object])

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

Parameters

  • 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, )
  • 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.

#.invoke(data: Object, [cb: function])

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
});

Parameters

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

#.invokeHttpTrigger(httpTriggerData,data: Object, [cb: function])

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.
});

Parameters

  • httpTrigger
    • object with reqBody. Simplifies building the entire http request object. Can override any parameters like headers.
  • 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.

Using with test frameworks (coming soon...)

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.

mocha

coming soon

chai

coming soon

chai-as-promised

coming soon

tape

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}`);
        });
});

Build

Clone this repository then run:

npm install 
npm test

License

MIT

Package Sidebar

Install

npm i azure-functions-node-harness

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • jsturtevant