Skip to content

Commit

Permalink
Add getMiniflareDurableObjectState() to environments, closes #157
Browse files Browse the repository at this point in the history
This allows easy direct construction of Durable Objects, for testing
ephemeral state and internal methods.
  • Loading branch information
mrbbot committed Sep 12, 2022
1 parent 765e543 commit cdaa62b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TestObject } from "./module-worker.js";

beforeAll(async () => {
const { TEST_OBJECT } = getMiniflareBindings();
const id = TEST_OBJECT.idFromName("test");
Expand All @@ -12,3 +14,13 @@ test("Durable Objects", async () => {
const res = await stub.fetch("https://object/");
expect(await res.text()).toBe("durable:https://object/:value");
});

test("Durable Objects direct", async () => {
// https://github.com/cloudflare/miniflare/issues/157
const env = getMiniflareBindings();
const id = env.TEST_OBJECT.idFromName("test");
const state = await getMiniflareDurableObjectState(id);
const object = new TestObject(state, env);
const res = await object.fetch(new Request("https://object/"));
expect(await res.text()).toBe("durable:https://object/:value");
});
21 changes: 17 additions & 4 deletions packages/shared-test-environment/src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@miniflare/core";
import {
DurableObjectId,
DurableObjectState,
DurableObjectStorage,
} from "@miniflare/durable-objects";
import { Context } from "@miniflare/shared";
Expand All @@ -27,6 +28,9 @@ declare global {
function getMiniflareDurableObjectStorage(
id: DurableObjectId
): Promise<DurableObjectStorage>;
function getMiniflareDurableObjectState(
id: DurableObjectId
): Promise<DurableObjectState>;
function getMiniflareFetchMock(): MockAgent;
function getMiniflareWaitUntil<WaitUntil extends any[] = unknown[]>(
event: FetchEvent | ScheduledEvent | ExecutionContext
Expand All @@ -41,6 +45,9 @@ export interface MiniflareEnvironmentUtilities {
getMiniflareDurableObjectStorage(
id: DurableObjectId
): Promise<DurableObjectStorage>;
getMiniflareDurableObjectState(
id: DurableObjectId
): Promise<DurableObjectState>;
getMiniflareFetchMock(): MockAgent;
getMiniflareWaitUntil<WaitUntil extends any[] = unknown[]>(
event: FetchEvent | ScheduledEvent | ExecutionContext
Expand All @@ -62,8 +69,14 @@ export async function createMiniflareEnvironmentUtilities(
},
async getMiniflareDurableObjectStorage(id: DurableObjectId) {
const plugin = (await mf.getPlugins()).DurableObjectsPlugin;
const storage = mf.getPluginStorage("DurableObjectsPlugin");
return plugin.getStorage(storage, id);
const factory = mf.getPluginStorage("DurableObjectsPlugin");
return plugin.getStorage(factory, id);
},
async getMiniflareDurableObjectState(id: DurableObjectId) {
const plugin = (await mf.getPlugins()).DurableObjectsPlugin;
const factory = mf.getPluginStorage("DurableObjectsPlugin");
const storage = plugin.getStorage(factory, id);
return new DurableObjectState(id, storage);
},
getMiniflareFetchMock() {
return fetchMock;
Expand All @@ -75,8 +88,8 @@ export async function createMiniflareEnvironmentUtilities(
},
async flushMiniflareDurableObjectAlarms(ids?: DurableObjectId[]) {
const plugin = (await mf.getPlugins()).DurableObjectsPlugin;
const storage = mf.getPluginStorage("DurableObjectsPlugin");
return plugin.flushAlarms(storage, ids);
const factory = mf.getPluginStorage("DurableObjectsPlugin");
return plugin.flushAlarms(factory, ids);
},
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { beforeAll, expect, test } from "vitest";
import { TestObject } from "./module-worker.js";
setupMiniflareIsolatedStorage();

beforeAll(async () => {
Expand All @@ -15,3 +16,13 @@ test("Durable Objects", async () => {
const res = await stub.fetch("https://object/");
expect(await res.text()).toBe("durable:https://object/:value");
});

test("Durable Objects direct", async () => {
// https://github.com/cloudflare/miniflare/issues/157
const env = getMiniflareBindings();
const id = env.TEST_OBJECT.idFromName("test");
const state = await getMiniflareDurableObjectState(id);
const object = new TestObject(state, env);
const res = await object.fetch(new Request("https://object/"));
expect(await res.text()).toBe("durable:https://object/:value");
});

0 comments on commit cdaa62b

Please sign in to comment.