Skip to main content

Wrangler's unstable_startWorker()

还没翻译译自英文(developers.cloudflare.com)本页 4 段都还没译

Caution

unstable_startWorker() is deprecated. Cloudflare recommends using the createTestHarness() API, which provides a harness specifically designed for integration testing.

The unstable_startWorker() API exposes the internals of the Wrangler dev server, and allows you to customize how it runs. Compared to using Miniflare directly for testing, you can pass in a Wrangler configuration file, and it will automatically load the configuration for you.

This example uses node:test, but should apply to any testing framework:

import assert from "node:assert";
import test, { after, before, describe } from "node:test";
import { unstable_startWorker } from "wrangler";

describe("worker", () => {
	let worker;

	before(async () => {
		worker = await unstable_startWorker({ config: "wrangler.json" });
	});

	test("hello world", async () => {
		assert.strictEqual(
			await (await worker.fetch("http://example.com")).text(),
			"Hello world",
		);
	});

	after(async () => {
		await worker.dispose();
	});
});