webmaxru/bpm-counter/azure-functions-test-mocking

Claude Code and agent skill by webmaxru/bpm-counter

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Patterns for unit testing Azure Functions with Jest, including module isolation for side-effect-heavy handlers

Install

npx vskill@latest install webmaxru/bpm-counter/azure-functions-test-mocking

SKILL.md

Context

Azure Functions handlers often have module-scope side effects (App Insights setup, database client init). Standard jest.mock() doesn't reset between tests, causing cross-test pollution. This skill covers the isolation pattern.

Patterns

1. Module isolation via jest.resetModules() + jest.isolateModules()
javascript
function loadHandler(mockOverrides) {
jest.resetModules(); // Clear stale module-scope state
let handler;
jest.isolateModules(() => {
jest.doMock('applicationinsights', () => ({ /* mocks */ }));
jest.doMock('@azure/cosmos', () => ({ /* mocks */ }), { virtual: true });
handler = require('./index');
});
return handler;
}
// Variant: test what happens when setup() THROWS at module load
function loadHandlerWithSetupCrash() {
jest.resetModules();
let handler;
jest.isolateModules(() => {
jest.doMock('applicationinsights', () => ({
setup: jest.fn(() => { throw new Error('Init failed'); }),
defaultClient: null,
}));
jest.doMock('@azure/cosmos', () => ({ /* mocks */ }), { virtual: true });
handler = require('./index');
});
return handler;
}
  • Call loadHandler() in each test for fresh module scope
  • Use jest.doMock() (not jest.mock()) inside isolateModules
  • Add { virtual: true } for packages not yet installed
  • jest.resetModules() before isolateModules is defensive best practice
2. Mock Azure Functions context
javascript
function createMockContext(overrides = {}) {
const log = jest.fn();
log.error = jest.fn();
log.warn = jest.fn();
log.info = jest.fn();
return {
log, res: null, bindings: {},
traceContext: { traceparent: 'test-trace-id' },
done: jest.fn(),
...overrides,
};
}
3. Falsy-valid edge cases

Always test { bpm: 0, isCorrect: false } to catch truthy-based validation bugs.

4. Client principal header testing
javascript
function base64Encode(obj) {
return Buffer.from(JSON.stringify(obj)).toString('base64');
}
// Test: valid, missing, garbage base64, valid-base64-of-invalid-JSON
5. Comprehensive early-return validation test
javascript
test('early return triggers ALL expected side effects', async () => {
const handler = loadHandler();
const context = createMockContext();
const req = createMockReq({ isCorrect: true }); // missing bpm
await handler(context, req);
expect(context.res.status).toBe(400); // correct status
expect(mockTrackException).toHaveBeenCalled(); // exception tracked
expect(mockCosmosCreate).not.toHaveBeenCalled(); // NO database write
expect(mockTrackEvent).not.toHaveBeenCalled(); // NO success event
});

Anti-Patterns

  • Top-level `jest.mock()` for module-scope side effects — leads to cached mocks across tests
  • Asserting `context.done()` was called — modern async functions should return, not context.done()
  • Defaulting `body = {}` in request helpers — hides the "missing body" edge case
  • Skipping `{ virtual: true }` — tests crash if mocked package isn't installed

Security

  • Security scanPassed, 90 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

7-day trend score
0

Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/webmaxru/bpm-counter/azure-functions-test-mocking/badge)](https://verified-skill.com/skills/webmaxru/bpm-counter/azure-functions-test-mocking)

Something wrong with this skill or its page? Tell us.