Skip to content

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

Closed
steven-ge007 wants to merge 1 commit into
moleculerjs:masterfrom
steven-ge007:feat/testing-helpers
Closed

feat: add testing helpers (createBroker, EventCatcher, MockingCalls)#1358
steven-ge007 wants to merge 1 commit into
moleculerjs:masterfrom
steven-ge007:feat/testing-helpers

Conversation

@steven-ge007
Copy link
Copy Markdown

Description

Implements the testing helpers proposed in RFC #3. This adds three new tools for writing tests in Moleculer microservices:

createBroker(opts, mockServices)

Factory function that creates a ServiceBroker pre-configured for testing with logging disabled, test: true flag, and both testing middlewares auto-registered.

EventCatcher middleware

Captures all emit, broadcast, and broadcastLocal events for test assertions:

broker.test.eventEmitted("user.created")
broker.test.eventEmittedTimes("user.created")
broker.test.eventEmittedWithParams("user.created", { role: "admin" })
broker.test.waitForEvent("async.event")
broker.test.clearEvents()

MockingCalls middleware

Intercepts broker.call for mocking and call tracking:

broker.test.mockAction("users.get").withParams({ id: 5 }).returns({ id: 5, name: "Mock" })
broker.test.actionCalled("users.get")
broker.test.actionCalledTimes("users.get")
broker.test.actionCalledWithParams("users.get", { id: 5 })
broker.test.clearActions()

Files changed

  • src/middlewares/testing.js - EventCatcher and MockingCalls middlewares
  • src/middlewares/index.js - Export Testing namespace
  • src/testing.js - createBroker factory function
  • src/testing.d.ts - TypeScript type definitions
  • index.js / index.mjs - Export Testing module
  • test/unit/middlewares/testing.spec.js - 22 test cases

Test Plan

All 2356 tests pass (137 suites), 22 new tests for the testing module.

Closes moleculerjs/rfcs#3

Implements RFC moleculerjs#3 - Testing helpers for Moleculer microservices.

- EventCatcher middleware: captures emit/broadcast/broadcastLocal events
- MockingCalls middleware: intercepts broker.call for mocking
- createBroker factory: pre-configured test broker with both middlewares
- Full TypeScript definitions
- 22 unit tests

Closes moleculerjs/rfcs#3

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@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