New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

stub-azure-function-context

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stub-azure-function-context

Provides an object similar to Function Runtime's context for use in unit testing

  • 1.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.4K
decreased by-8.22%
Maintainers
2
Weekly downloads
 
Created
Source

stub-azure-function-context

Build Status

Aims to implement the context object as described on the Azure Functions JavaScript Developer Guide.

Usage

By default the behaviour assumes a req HTTP trigger and a res HTTP output, but if you have more exotic config, then you can set that up!

If you follow the "ordered argument" pattern then you'll need to define the triggers/outputs in the same order as in your function.json.

From 1.0.3 this library supports endpoint handlers returning Promise objects, and by extension, the async/await style.

stubContext resolves with an object containing:

{
    context: context, // the actual stubbed context object after your function manipulates it
    err: null|Error,  // any error thrown or passed to context.done; eg: context.done(new Error("Oops"))
    propertyBag: {},  // object of any overrides you want to make on your outputs
}

By default, logging uses console as a backend, although you can import and use setContextLogger to set your own.

Only the methods defined in the developer guide are available in the stubContext call:

  • error
  • warn
  • info
  • verbose

Usage examples:


const { stubContext, setContextLogger } = require('stub-azure-function-context');
const functionToTest = require('../function-under-test');

// Optional step to direct context.log output elsewhere:
const logger = require('./your-own-logger');
setContextLogger(logger);

describe('app code', () => {
	it('returns 200', async () => {
	    const { context, err, propertyBag, } = await stubContext(functionToTest);
	    expect(context).to.have.nested.property('res.status', 200);
	});
	it('returns 200 in promise/a+ style', (done) => {
		stubContext(functionToTest)
			.then(({ context, err, propertyBag }) => {
				expect(context).to.have.nested.property('res.status', 200);
				done();
			})
			.catch(done);
	});
	it('change trigger values before calling your function under test', async () => {
		const { context } = await stubContext((context, req, ...otherTriggers) => {
		    req.body = { 'helpful': 'test object' };
		    return functionToTest(context, req, ...otherTriggers);
		});
		expect(context).to.have.nested.property('res.body.helpful', 'test object');
	});
});

Keywords

FAQs

Package last updated on 10 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc