Skip to content

feat(testing): add Testing helpers (createBroker, EventCatcher, MockingCalls)#1346

Closed
aifunmobi wants to merge 6 commits into
moleculerjs:masterfrom
aifunmobi:feat/testing-helpers
Closed

feat(testing): add Testing helpers (createBroker, EventCatcher, MockingCalls)#1346
aifunmobi wants to merge 6 commits into
moleculerjs:masterfrom
aifunmobi:feat/testing-helpers

Conversation

@aifunmobi
Copy link
Copy Markdown

Summary

Implements RFC #3 — Testing helpers for Moleculer.

  • createBroker(options, mockServices) — Creates a real ServiceBroker with test middlewares pre-configured (logger disabled, test mode on)
  • EventCatcher middleware — Intercepts emit/broadcast/broadcastLocal and records all events for assertion
  • MockingCalls middleware — Intercepts broker.call, supports fluent mock API with param/meta matching

Usage

const { Testing } = require("moleculer");
const { createBroker } = Testing;

const broker = createBroker({}, [
  { name: "posts", actions: { list: jest.fn(async () => [1, 2, 3]) } }
]);

await broker.start();
await broker.call("posts.list", { limit: 5 });
// Assert events
expect(broker.test.eventEmitted("posts.updated")).toBe(true);
// Mock actions
broker.test.mockAction("users.get").withParams({ id: 5 }).returnValue({ name: "John" });

API (broker.test.*)

Events: eventEmitted, eventEmittedTimes, eventEmittedWithParams, waitForEvent, getEvents, clearEvents

Actions: mockAction (fluent: .withParams(), .withMeta(), .returnValue(), .rejectWith()), actionCalled, actionCalledTimes, actionCalledWithParams, getCalls, clearActions, clearMocks

Utility: clearAll

Test plan

  • 22 tests covering all API methods
  • Tests for createBroker with options, mock services, custom middlewares
  • Tests for EventCatcher: emit, broadcast, broadcastLocal, waitForEvent, timeout
  • Tests for MockingCalls: mock with params, reject, clear, getCalls
  • Export verification tests

Design decisions

  • Uses real ServiceBroker (not a fake) — middlewares wrap actual broker methods
  • Middlewares follow existing moleculer middleware pattern (emit/broadcast/call hooks)
  • Integrated at require("moleculer").Testing as specified in the RFC
  • broker.test namespace avoids conflicts with existing broker API

Closes moleculerjs/rfcs#3

🤖 Generated with Claude Code

…ngCalls)

Implements RFC moleculerjs#3 - Testing helpers for Moleculer.

- createBroker(options, mockServices): Creates a real ServiceBroker with
  test middlewares pre-configured (logger disabled, test mode enabled)
- EventCatcher middleware: Intercepts emit/broadcast/broadcastLocal,
  provides broker.test.eventEmitted(), waitForEvent(), etc.
- MockingCalls middleware: Intercepts broker.call, supports mock responses
  with fluent API: mockAction("name").withParams({}).returnValue({})
- Exported via require("moleculer").Testing

API surface (broker.test.*):
  Events: eventEmitted, eventEmittedTimes, eventEmittedWithParams,
          waitForEvent, getEvents, clearEvents
  Actions: mockAction, actionCalled, actionCalledTimes,
           actionCalledWithParams, getCalls, clearActions, clearMocks
  Utility: clearAll

Closes moleculerjs/rfcs#3

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
aifunmobi and others added 5 commits February 8, 2026 18:34
Replace optional catch binding (`catch {}`) with `catch (_err)` to
match the codebase convention and satisfy Codacy static analysis.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use parameterless catch blocks instead of catch (_err) to
avoid unused variable warnings flagged by Codacy static analysis.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Assign middleware object to variable before returning to avoid
PMD misinterpreting the object literal as a block statement.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@aifunmobi
Copy link
Copy Markdown
Author

Friendly ping — all checks are passing. Happy to address any feedback!

@aifunmobi
Copy link
Copy Markdown
Author

@icebob Friendly follow-up — this implements the testing helpers from RFC #3 (createBroker, EventCatcher, MockingCalls). All Codacy checks are green. Would love any feedback!

@icebob-ai
Copy link
Copy Markdown
Contributor

Closing this PR. There are multiple duplicate PRs implementing the same RFC #3, and we'll be handling the testing helpers implementation internally as part of the v0.15 release. Thank you for your interest in contributing to Moleculer.

@icebob-ai icebob-ai closed this May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Testing helpers

2 participants