Skip to content

jsturtevant/azure-functions-node-harness

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure Functions Node Harness

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.

Install

coming soon to npm.

for now: npm install jsturtevant/azure-functions-node-harness --save-dev

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.

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

Build

Clone this repository then run:

npm install 
npm test

License

MIT

About

Easily invoke your NodeJS Azure Functions during unit tests

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%