npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

azure-functions-node-harness

v0.1.1

Published

Easily invoke your NodeJS Azure Functions during unit tests

Downloads

5

Readme

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