Test APIs
The Workers Vitest integration provides runtime helpers for writing tests in the cloudflare:test
module. The cloudflare:test
module is provided by the @cloudflare/vitest-pool-workers
package, but can only be imported from test files that execute in the Workers runtime.
-
env
: import("cloudflare:test").ProvidedEnv-
Exposes the
env
object for use as the second argument passed to ES modules format exported handlers. This provides access to bindings that you have defined in your Vitest configuration file.
To configure the type of this value, use an ambient module type:
-
-
SELF
: Fetcher-
Service binding to the default export defined in the
main
Worker. Use this to write integration tests against your Worker. Themain
Worker runs in the same isolate/context as tests so any global mocks will apply to it too.
-
-
fetchMock
: import("undici").MockAgent-
Declarative interface for mocking outbound
fetch()
requests. Deactivated by default and reset before running each test file. Refer toundici
'sMockAgent
documentation ↗ for more information. Note this only mocksfetch()
requests for the current test runner Worker. Auxiliary Workers should mockfetch()
es using the MiniflarefetchMock
/outboundService
options. Refer to Configuration for more information.
-
-
createExecutionContext()
: ExecutionContext- Creates an instance of the
context
object for use as the third argument to ES modules format exported handlers.
- Creates an instance of the
-
waitOnExecutionContext(ctx:ExecutionContext)
: Promise<void>-
Use this to wait for all Promises passed to
ctx.waitUntil()
to settle, before running test assertions on any side effects. Only accepts instances ofExecutionContext
returned bycreateExecutionContext()
.
-
-
createScheduledController(options?:FetcherScheduledOptions)
: ScheduledController-
Creates an instance of
ScheduledController
for use as the first argument to modules-formatscheduled()
exported handlers.
-
-
createMessageBatch(queueName:string, messages:ServiceBindingQueueMessage[])
: MessageBatch- Creates an instance of
MessageBatch
for use as the first argument to modules-formatqueue()
exported handlers.
- Creates an instance of
-
getQueueResult(batch:MessageBatch, ctx:ExecutionContext)
: Promise<FetcherQueueResult>-
Gets the acknowledged/retry state of messages in the
MessageBatch
, and waits for allExecutionContext#waitUntil()
edPromise
s to settle. Only accepts instances ofMessageBatch
returned bycreateMessageBatch()
, and instances ofExecutionContext
returned bycreateExecutionContext()
.
-
-
runInDurableObject<O extends DurableObject, R>(stub:DurableObjectStub, callback:(instance: O, state: DurableObjectState) => R | Promise<R>)
: Promise<R>-
Runs the provided
callback
inside the Durable Object that corresponds to the providedstub
.
This temporarily replaces your Durable Object's
fetch()
handler withcallback
, then sends a request to it, returning the result. This can be used to call/spy-on Durable Object methods or seed/get persisted data. Note this can only be used withstub
s pointing to Durable Objects defined in themain
Worker.
-
-
runDurableObjectAlarm(stub:DurableObjectStub)
: Promise<boolean>- Immediately runs and removes the Durable Object pointed to by
stub
's alarm if one is scheduled. Returnstrue
if an alarm ran, andfalse
otherwise. Note this can only be used withstub
s pointing to Durable Objects defined in themain
Worker.
- Immediately runs and removes the Durable Object pointed to by
-
listDurableObjectIds(namespace:DurableObjectNamespace)
: Promise<DurableObjectId[]>-
Gets the IDs of all objects that have been created in the
namespace
. RespectsisolatedStorage
if enabled, meaning objects created in a different test will not be returned.
-
-
applyD1Migrations(db:D1Database, migrations:D1Migration[], migrationTableName?:string)
: Promise<void>- Applies all un-applied D1 migrations stored in the
migrations
array to databasedb
, recording migrations state in themigrationsTableName
table.migrationsTableName
defaults tod1_migrations
. Call thereadD1Migrations()
function from the@cloudflare/vitest-pool-workers/config
package inside Node.js to get themigrations
array. Refer to the D1 recipe ↗ for an example project using migrations.
- Applies all un-applied D1 migrations stored in the