Pular para o conteúdo

Wrangler's unstable_startWorker()

Atualizado em Ver como Markdown

If you do not want to use Vitest, consider using Wrangler's unstable_startWorker() API. This API exposes the internals of Wrangler's dev server, and allows you to customise 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();
	});
});