You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At the moment, miniflare supports testing durable object stubs. However, I'd like to instantiate a durable object class and unit test the non-fetch() methods.
At the moment, this is accomplishable but could be more ergonomic. After looking at the miniflare source, I was able to put together the following:
import{DurableObjectState}from'@miniflare/durable-objects';import{Campaign}from'../src/campaign';// assume this is a class that implements DurableObjectconstenv=getMiniflareBindings();constid=env.CAMPAIGN.newUniqueId();describe('Campaign',()=>{letstorage: DurableObjectStorage;letcampaign: Campaign;beforeAll(async()=>{storage=awaitgetMiniflareDurableObjectStorage(id);});beforeEach(()=>{campaign=newCampaign(newDurableObjectState(idasany,storageasany),env);});test('getPeople',async()=>{expect(awaitcampaign.getPeople()).toBe(0);storage.put('totalPeople',2);expect(awaitcampaign.getPeople()).toBe(2);});});
Notice the need to cast to any in new DurableObjectState(id as any, storage as any) (because the DurableObjectId returned from DurableObjectNamespace#newUniqueId() is not the same DurableObjectId typescript type that DurableObjectState expects. Also note that I had to look in the miniflare source to figure out how to instantiate new DurableObjectState().
An improvement would be exposing a getMiniflareDurableObjectState() function like the following:
import{DurableObjectState}from'@miniflare/durable-objects';import{Campaign}from'../src/campaign';// assume this is a class that implements DurableObjectconstenv=getMiniflareBindings();constid=env.CAMPAIGN.newUniqueId();describe('Campaign',()=>{letstorage: DurableObjectStorage;letcampaign: Campaign;beforeAll(async()=>{storage=awaitgetMiniflareDurableObjectStorage(id);});beforeEach(async()=>{campaign=newCampaign(getMiniflareDurableObjectState(id,storage),env);// orcampaign=newCampaign(awaitgetMiniflareDurableObjectState(id),env);});});
The text was updated successfully, but these errors were encountered:
Note: while previously I was able to create my own getMiniflareDurableObjectState() function to test non-fetch methods, at some point in the past month this appears to have been broken. See issue #184. It's unclear what has caused this regression and I haven't actually been able to recreate a "working" getMiniflareDurableObjectState()--even when using previous versions of the miniflare library.
At the moment, miniflare supports testing durable object stubs. However, I'd like to instantiate a durable object class and unit test the non-
fetch()
methods.At the moment, this is accomplishable but could be more ergonomic. After looking at the miniflare source, I was able to put together the following:
Notice the need to cast to
any
innew DurableObjectState(id as any, storage as any)
(because theDurableObjectId
returned fromDurableObjectNamespace#newUniqueId()
is not the sameDurableObjectId
typescript type thatDurableObjectState
expects. Also note that I had to look in the miniflare source to figure out how to instantiatenew DurableObjectState()
.An improvement would be exposing a
getMiniflareDurableObjectState()
function like the following:Usage like
The text was updated successfully, but these errors were encountered: