-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: allow access to the Durable Object's instance from inside a test #385
Conversation
This allows access to the Durable Object instance for direct manipulation and testing
@mrbbot I'm curious about how the architecture will look, when using Miniflare with the OSS runtime. When running tests, will it be the case that the Durable Object are run inside the OSS runtime (V8), but Miniflare still runs Jest inside Node's JS context? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey! 👋 Thanks for raising this use case, and opening the PR! 🙂 I've added a comment about input/output gating. Interested to hear your thoughts, but otherwise happy to merge this. ✅
const instance = await getMiniflareDurableObjectInstance(id); | ||
const fetch = jest.spyOn(instance, "fetch"); | ||
|
||
await stub.fetch(new Request("https://object/")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem here, is that fetch
won't run under the input/output gates, so concurrent calls to fetch
may produce invalid results. I'm not too sure what the best solution here is:
- We could ask users to also do
const state = getMiniflareDurableObjectState()
, and userunWithMiniflareDurableObjectGates()
as above, but reflecting on that API now, it feels a little too verbose. - We could wrap use a
Proxy
or something ongetMiniflareDurableObjectInstance()
to interceptfetch()
calls and apply input/output gates to them, but this might break mocks/spies.
Maybe option 1. is fine for now. What do you think?
|
|
cdaa62b added the ability to directly construct a Durable Object, which is great for testing a DO in isolation but doesn't allow for easily testing Durable Objects that communicate with other Durable Objects.
Without access to the internal instance that
stub.fetch(...)
uses, it isn't easy to test request that span across multiple Durable Objects.For example, given this example of an object that makes a sub request to another object:
The change in this PR allows writing a test that can introspect the behaviour of
OtherObject
, even though the initial request goes toTestObject
, like this: