This repository was archived by the owner on Mar 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +69
-1
lines changed
jest-environment-miniflare/test/fixtures
shared-test-environment/src
vitest-environment-miniflare/test/fixtures/modules Expand file tree Collapse file tree 5 files changed +69
-1
lines changed Original file line number Diff line number Diff line change @@ -267,6 +267,15 @@ export class DurableObjectsPlugin
267267 } ) . runWith ( ( ) => state [ kAlarm ] ( ) ) ;
268268 }
269269
270+ getObjects (
271+ storageFactory : StorageFactory ,
272+ namespace : string
273+ ) : DurableObjectId [ ] {
274+ return [ ...this . #objectStates. keys ( ) ]
275+ . map ( getObjectIdFromKey )
276+ . filter ( ( id ) => id [ kObjectName ] === namespace ) ;
277+ }
278+
270279 async beforeReload ( ) : Promise < void > {
271280 // Clear instance map, this should cause old instances to be GCed
272281 this . #objectStorages. clear ( ) ;
Original file line number Diff line number Diff line change 1+ import { DurableObjectId } from "@miniflare/durable-objects" ;
2+
13beforeAll ( async ( ) => {
24 const { TEST_OBJECT } = getMiniflareBindings ( ) ;
35 const id = TEST_OBJECT . idFromName ( "test" ) ;
@@ -43,3 +45,25 @@ test("Durable Objects direct", async () => {
4345 expect ( await res1 . text ( ) ) . toBe ( "0" ) ;
4446 expect ( await res2 . text ( ) ) . toBe ( "1" ) ;
4547} ) ;
48+
49+ test ( "Durable Objects list" , async ( ) => {
50+ const env = getMiniflareBindings ( ) ;
51+
52+ // From beforeAll
53+ expect ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) . toHaveLength ( 1 ) ;
54+
55+ const id = env . TEST_OBJECT . idFromName ( "test" ) ;
56+ env . TEST_OBJECT . get ( id ) ;
57+ expect ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) . toHaveLength ( 1 ) ;
58+ expect ( ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) [ 0 ] ) . toMatchObject (
59+ new DurableObjectId ( "TEST_OBJECT" , id . toString ( ) )
60+ ) ;
61+
62+ const id2 = env . TEST_OBJECT . idFromName ( "test2" ) ;
63+ const stub = env . TEST_OBJECT . get ( id2 ) ;
64+ await stub . fetch ( "https://object/" ) ;
65+ expect ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) . toHaveLength ( 2 ) ;
66+ expect ( ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) [ 1 ] ) . toMatchObject (
67+ new DurableObjectId ( "TEST_OBJECT" , id2 . toString ( ) )
68+ ) ;
69+ } ) ;
Original file line number Diff line number Diff line change @@ -43,6 +43,9 @@ declare global {
4343 function flushMiniflareDurableObjectAlarms (
4444 ids : DurableObjectId [ ]
4545 ) : Promise < void > ;
46+ function getMiniflareDurableObjectIds (
47+ namespace : string
48+ ) : Promise < DurableObjectId [ ] > ;
4649}
4750
4851export interface MiniflareEnvironmentUtilities {
@@ -62,6 +65,7 @@ export interface MiniflareEnvironmentUtilities {
6265 event : FetchEvent | ScheduledEvent | ExecutionContext
6366 ) : Promise < WaitUntil > ;
6467 flushMiniflareDurableObjectAlarms ( ids : DurableObjectId [ ] ) : Promise < void > ;
68+ getMiniflareDurableObjectIds ( namespace : string ) : Promise < DurableObjectId [ ] > ;
6569}
6670
6771export async function createMiniflareEnvironmentUtilities (
@@ -106,5 +110,12 @@ export async function createMiniflareEnvironmentUtilities(
106110 const factory = mf . getPluginStorage ( "DurableObjectsPlugin" ) ;
107111 return plugin . flushAlarms ( factory , ids ) ;
108112 } ,
113+ async getMiniflareDurableObjectIds (
114+ namespace : string
115+ ) : Promise < DurableObjectId [ ] > {
116+ const plugin = ( await mf . getPlugins ( ) ) . DurableObjectsPlugin ;
117+ const factory = mf . getPluginStorage ( "DurableObjectsPlugin" ) ;
118+ return plugin . getObjects ( factory , namespace ) ;
119+ } ,
109120 } ;
110121}
Original file line number Diff line number Diff line change 1+ import { DurableObjectId } from "@miniflare/durable-objects" ;
12import { beforeAll , expect , test } from "vitest" ;
23setupMiniflareIsolatedStorage ( ) ;
34
@@ -46,3 +47,25 @@ test("Durable Objects direct", async () => {
4647 expect ( await res1 . text ( ) ) . toBe ( "0" ) ;
4748 expect ( await res2 . text ( ) ) . toBe ( "1" ) ;
4849} ) ;
50+
51+ test ( "Durable Objects list" , async ( ) => {
52+ const env = getMiniflareBindings ( ) ;
53+
54+ // From beforeAll
55+ expect ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) . toHaveLength ( 1 ) ;
56+
57+ const id = env . TEST_OBJECT . idFromName ( "test" ) ;
58+ env . TEST_OBJECT . get ( id ) ;
59+ expect ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) . toHaveLength ( 1 ) ;
60+ expect ( ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) [ 0 ] ) . toMatchObject (
61+ new DurableObjectId ( "TEST_OBJECT" , id . toString ( ) )
62+ ) ;
63+
64+ const id2 = env . TEST_OBJECT . idFromName ( "test2" ) ;
65+ const stub = env . TEST_OBJECT . get ( id2 ) ;
66+ await stub . fetch ( "https://object/" ) ;
67+ expect ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) . toHaveLength ( 2 ) ;
68+ expect ( ( await getMiniflareDurableObjectIds ( "TEST_OBJECT" ) ) [ 1 ] ) . toMatchObject (
69+ new DurableObjectId ( "TEST_OBJECT" , id2 . toString ( ) )
70+ ) ;
71+ } ) ;
Original file line number Diff line number Diff line change @@ -23,7 +23,8 @@ function isMiniflarePkg(name) {
2323 return (
2424 name . startsWith ( scope ) ||
2525 name === "miniflare" ||
26- name === "jest-environment-miniflare"
26+ name === "jest-environment-miniflare" ||
27+ name === "vitest-environment-miniflare"
2728 ) ;
2829}
2930
You can’t perform that action at this time.
0 commit comments