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
1
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.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.4K
decreased by-8.22%
Maintainers
1
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.

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!

NB: 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.

If your function under test does not call context.done, then your test will time out. This is intended functionality so that you can find cases where you don't return back to the Function Runtime.

stubContext resolves with an object containing:

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

Logging goes out to console. Only the methods defined in the developer guide are mentioned:

  • error
  • warn
  • info
  • verbose

None of that silly stuff ;-)

Usage examples:


const { stubContext } = require('stub-azure-function-context');

const functionToTest = require('../function-under-test');

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 May 2018

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