For most users, Cloudflare recommends using the Workers Vitest integration. If you have been usingunstable_dev(), refer to theMigrate fromunstable_dev() guide.
unstable_startWorker() is an experimental API subject to breaking changes.
If you do not want to use Vitest, consider usingWrangler'sunstable_startWorker() API. This API exposes the internals of Wrangler's dev server, and allows you to customise how it runs. Compared to usingMiniflare directly for testing, you can pass in a Wrangler configuration file, and it will automatically load the configuration for you.
This example usesnode:test, but should apply to any testing framework:
importassert from"node:assert";importtest,{after,before,describe} from"node:test";import{unstable_startWorker} from"wrangler";describe("worker",()=>{letworker;before(async()=>{worker=awaitunstable_startWorker({ config:"wrangler.json"});});test("hello world",async()=>{assert.strictEqual(await (awaitworker.fetch("http://example.com")).text(),"Hello world",);});after(async()=>{awaitworker.dispose();});});