![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
stub-azure-function-context
Advanced tools
Provides an object similar to Function Runtime's context for use in unit testing
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!
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
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');
});
});
FAQs
Provides an object similar to Function Runtime's context for use in unit testing
The npm package stub-azure-function-context receives a total of 1,987 weekly downloads. As such, stub-azure-function-context popularity was classified as popular.
We found that stub-azure-function-context demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.