Migrate from Miniflare 2's test environments
Miniflare 2 ↗ provided custom environments for Jest and Vitest in the jest-environment-miniflare
and vitest-environment-miniflare
packages respectively.
The @cloudflare/vitest-pool-workers
package provides similar functionality using modern Miniflare versions and the workerd
runtime ↗. workerd
is the same JavaScript/WebAssembly runtime that powers Cloudflare Workers. Using workerd
practically eliminates behavior mismatches between your tests and deployed code. Refer to the Miniflare 3 announcement ↗ for more information.
First, you will need to uninstall the old environment and install the new pool. Vitest environments can only customize the global scope, whereas pools can run tests using a completely different runtime. In this case, the pool runs your tests inside workerd
↗ instead of Node.js.
After installing the Workers Vitest configuration, update your Vitest configuration file to use the pool instead. Most Miniflare configuration previously specified environmentOptions
can be moved to poolOptions.workers.miniflare
instead. Refer to Miniflare’s WorkerOptions
interface ↗ for supported options and the Miniflare version 2 to 3 migration guide ↗ for more information. If you relied on configuration stored in a wrangler.toml
file, set wrangler.configPath
too.
If you are using TypeScript, update your tsconfig.json
to include the correct ambient types
:
To access bindings in your tests, use the env
helper from the cloudflare:test
module.
If you are using TypeScript, add an ambient .d.ts
declaration file defining a ProvidedEnv
interface
in the cloudflare:test
module to control the type of env
:
Isolated storage is now enabled by default. You no longer need to include setupMiniflareIsolatedStorage()
in your tests.
The new ExecutionContext()
constructor and getMiniflareWaitUntil()
function are now createExecutionContext()
and waitOnExecutionContext()
respectively. Note waitOnExecutionContext()
now returns an empty Promise<void>
instead of a Promise
resolving to the results of all waitUntil()
ed Promise
s.
The getMiniflareFetchMock()
function has been replaced with the new fetchMock
helper from the cloudflare:test
module. fetchMock
has the same type as the return type of getMiniflareFetchMock()
. There are a couple of differences between fetchMock
and the previous return value of getMiniflareFetchMock()
:
fetchMock
is deactivated by default, whereas previously it would start activated. This deactivation prevents unnecessary buffering of request bodies if you are not usingfetchMock
. You will need to callfetchMock.activate()
before callingfetch()
to enable it.fetchMock
is reset at the start of each test run, whereas previously, interceptors added in previous runs would apply to the current one. This ensures test runs are not affected by previous runs.
The getMiniflareDurableObjectStorage()
, getMiniflareDurableObjectState()
, getMiniflareDurableObjectInstance()
, and runWithMiniflareDurableObjectGates()
functions have all been replaced with a single runInDurableObject()
function from the cloudflare:test
module. The runInDurableObject()
function accepts a DurableObjectStub
with a callback accepting the Durable Object and corresponding DurableObjectState
as arguments. Consolidating these functions into a single function simplifies the API surface, and ensures instances are accessed with the correct request context and gating behavior ↗. Refer to the Test APIs page for more details.
The flushMiniflareDurableObjectAlarms()
function has been replaced with the runDurableObjectAlarm()
function from the cloudflare:test
module. The runDurableObjectAlarm()
function accepts a single DurableObjectStub
and returns a Promise
that resolves to true
if an alarm was scheduled and the alarm()
handler was executed, or false
otherwise. To “flush” multiple instances’ alarms, call runDurableObjectAlarm()
in a loop.
Finally, the getMiniflareDurableObjectIds()
function has been replaced with the listDurableObjectIds()
function from the cloudflare:test
module. The listDurableObjectIds()
function now accepts a DurableObjectNamespace
instance instead of a namespace string
to provide stricter typing. Note the listDurableObjectIds()
function now respects isolated storage. If enabled, IDs of objects created in other tests will not be returned.