Repository navigation
add handler and workflow test helpers - #338
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
adenhertog
force-pushed
the
issue-265-test-helpers
branch
from
October 4, 2026 05:49
21d0d4f to
d4045f5
Compare
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This was referenced Oct 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #265
Summary
Adds
handlerContext(), a recordingworkflowContext(), atestWorkflow()scenario runner andInMemoryQueue.idle()to bus-core, and stops the bus sleeping 500 ms after an empty read that already waited.Background
Design principle 5 says handlers are testable as plain functions with a fake context, without a bus or mocking framework. Only
workflowContext()existed, and it recorded nothing, so tests hand-rolled fakes or ran a real bus and waited on anEventEmitter.Problem
HandlerContextfake, and no way to see what a handler sent, published or replied with (or itsSendOptions, such asdeliverAfter), or whether it failed or returned the message.BusInstance.applicationLoopslept 500 ms on top of the transport's own wait, so a message sent to an idle in-memory or SQS bus waited up to 500 ms more.Approach
Each acceptance criterion, checked against master:
handlerContext()andworkflowContext()record sends and publishes with their options: added. Newsrc/testing/folder, exported from the root. Both recordsent,publishedandreplied; each reply also records itsdestination.sentOf(Type),publishedOf(Type)andrepliedOf(Type)narrow the records to one type, so a test can read their fields.workflowContext()also recordscompleted/discarded, and takes itscorrelationIdandreplyTofromattributes. Overrides are aPartial<HandlerContext>plusreplyTo, so an optional member that add a durable transactional outbox with withOutbox() and bus.transaction() #335 adds (transaction) can be set without changing the fakes.testWorkflowcovers start, continue, complete, discard and a timeout fromadvanceTime, for both workflow styles: added. It haswhen,givenandadvanceTime, plusstate,nowandscheduled. The registry's merge rule is now inworkflow/apply-workflow-state-change.ts, shared by the registry and the runner. The runner also follows the bus on:hasLookupValuefrom return no workflow state for a lookup without a value on every persistence #337;failMessage/returnMessage, after which nothing is saved or sent;$version, which counts saves.InMemoryQueue.idle()replaces the EventEmitter waits in an existing integration test: done (handler-for.integration.ts)./guide/testingpage. Every changed snippet was run, not just type-checked.Review findings. Each was reproduced against the code before it was fixed:
resolveDueAtmoved into an internalassertDeliveryOptions(), which the bus and every fake call. A NaN or negativedeliverAfter, an invaliddeliverAt, or both together throwInvalidDeliveryOptions.replythrowsDelayedReplyNotSupportedwhen it's given delivery options.destination, which isTEST_RETURN_ADDRESSunless areplyTois given.replyTo: undefinedmakesreplythrowReturnAddressMissing, which names the handled message intestWorkflow.when()gives messagesTEST_RETURN_ADDRESSas theirreplyTounless the caller sets one.sentOf,publishedOfandrepliedOffilter byNAMEand return the narrowed type, andreplied[].optionsis typedPartial<MessageAttributes>(RecordedReply). Narrowingwhen(message)to the messages a workflow handles isn't cheap, becauseFunctionWorkflowdoesn't carry its message types, so that part is left out.idle()no longer hangs afterdispose(). It rejects with the newInMemoryQueueDisposed, both while it's waiting and when it's called after disposal with messages left. This is documented.mapsToInstanceuseshasLookupValue.workflowContext()takes its defaults from its attributes.correlationIdandreplyTodefault to those ofattributes.advanceTime()no longer loses messages.failMessage()and then throws is the exception: the bus dead-letters that message, so the scenario drops it (re-review fix).returnMessage()stays scheduled and is delivered again the next time the clock moves.replyTothe bus would stamp.createWorkflow,testWorkflow()throws the newWorkflowFactoryMissing, with help text. The check reads the constructor's.length, so aclass Derived extends Base {}whoseBasetakes dependencies isn't caught. ThecreateWorkflowdocs say so.RecordingHandlerContextand in the guide. A send or reply made after the handler resolved is still recorded; on a bus it's sent straight away, dropped, or throwsReplyOutsideHandlingContext.HandlerContext/BusSenderJSDoc examples now usehandlerContext(), and the specs buildsutinbeforeAll.For review:
when()adds the instance'sworkflowIdto awhenmessage unless the caller sets one, as messages sent from the workflow and replies to them carry it. As a result, an external event that relies on the default mapping by mistake reaches the instance in the scenario, though it wouldn't on a bus.The scenario follows one instance. A second
startedBymessage starts a new one, and the scenario follows that instead.This is original work under the clean-room policy, not ported, translated or copied from another messaging framework
Added a changeset (
pnpm changeset) for user-facing changes to published packages, or none is neededDocs: updated
docs/for user-facing changes, or none needed🤖 Generated with Claude Code