← Cloudflare Workers / workers / testing / vitest-integration / migration-guides
Migrace z testovacích prostředí Miniflare 2
Miniflare 2 ↗ poskytovala vlastní prostředí pro Jest a Vitest v jest-environment-miniflare a vitest-environment-miniflare balíčky (v tomto pořadí).
Balíček @cloudflare/vitest-plugin balíček poskytuje podobnou funkcionalitu pomocí moderních verzí Miniflare a workerd runtime ↗. workerd je stejný JavaScript/WebAssembly runtime, který pohání Cloudflare Workers. Použitím workerd snižuje rozdíly v chování mezi vašimi testy a nasazeným kódem. Přečtěte si Oznámení Miniflare 3 ↗ s dalšími informacemi.
Nainstalujte integraci Workers Vitest
Nejprve odinstalujte staré prostředí a nainstalujte plugin Vitest. Prostředí Vitest umí přizpůsobit pouze globální rozsah, zatímco plugin spouští testy pomocí jiného runtime. V tomto případě plugin spouští vaše testy uvnitř workerd ↗ místo Node.js.
npm uninstall vitest-environment-miniflare
npm install --save-dev vitest@^4.1.0
npm install --save-dev @cloudflare/vitest-pluginAktualizujte konfigurační soubor Vitest
Po instalaci integrace Workers Vitest upravte konfigurační soubor Vitest tak, aby používal cloudflareTest() Vite plugin. Většinu konfigurace Miniflare, která se dříve zadávala v environmentOptions lze přesunout do miniflare možnost v cloudflareTest(). Viz Miniflare WorkerOptions rozhraní ↗ pro podporované možnosti a Průvodce migrací Miniflare z verze 2 na 3 další informace. Pokud jste se spoléhali na konfiguraci uloženou v souboru Wrangler, nastavte wrangler.configPath také.
+ import { cloudflareTest } from "@cloudflare/vitest-plugin";
+ import { defineConfig } from "vitest/config";
- export default defineWorkersConfig({
- test: {
- environment: "miniflare",
- environmentOptions: { ... },
- },
- });
+ export default defineConfig({
+ plugins: [
+ cloudflareTest({
+ miniflare: { ... },
+ wrangler: { configPath: "./wrangler.jsonc" },
+ }),
+ ],
+ });Aktualizujte konfigurační soubor TypeScript
Pokud používáte TypeScript, aktualizujte tsconfig.json abyste zahrnuli správné ambientní types:
{
"compilerOptions": {
...,
"types": [
...
- "vitest-environment-miniflare/globals"
+ "@cloudflare/vitest-plugin/types"
]
},
}Přístup k bindings
Pro přístup k vazby ve vašich testech použijte env pomocnou funkci z cloudflare:workers modul.
import { it } from "vitest";
+ import { env } from "cloudflare:workers";
it("does something", () => {
- const env = getMiniflareBindings();
// ...
});Pokud používáte TypeScript, musíte definovat typ env pro vaše testy. Podrobnosti najdete v Definujte typy pro pokyny k nastavení.
Izolace úložiště
Izolace úložiště je ve výchozím nastavení podle testovacího souboru. Již není nutné vkládat setupMiniflareIsolatedStorage() ve vašich testech.
- const describe = setupMiniflareIsolatedStorage();
+ import { describe } from "vitest";Práce s waitUntil()
new ExecutionContext() konstruktor a getMiniflareWaitUntil() funkce jsou nyní createExecutionContext() a waitOnExecutionContext() v uvedeném pořadí. Vezměte na vědomí waitOnExecutionContext() nyní vrací prázdný Promise<void> místo Promise která se přeloží na výsledky všech waitUntil()ových Promises.
+ import { createExecutionContext, waitOnExecutionContext } from "cloudflare:test";
it("does something", () => {
// ...
- const ctx = new ExecutionContext();
+ const ctx = createExecutionContext();
const response = worker.fetch(request, env, ctx);
- await getMiniflareWaitUntil(ctx);
+ await waitOnExecutionContext(ctx);
});Mockování odchozích požadavků
getMiniflareFetchMock() funkce už není k dispozici. Pro simulaci odchozích požadavků použijte @msw/cloudflare ↗. Viz Mockování odchozích požadavků pro pokyny k nastavení.
Použijte pomocné funkce Durable Object
getMiniflareDurableObjectStorage(), getMiniflareDurableObjectState(), getMiniflareDurableObjectInstance(), a runWithMiniflareDurableObjectGates() funkce byly všechny nahrazeny jedinou runInDurableObject() funkce z cloudflare:test modul. runInDurableObject() funkce přijímá DurableObjectStub s callbackem, který přijímá Durable Object a odpovídající DurableObjectState jako argumenty. Sloučením těchto funkcí do jedné funkce se zjednoduší povrch API a zajistí se, že se k instancím přistupuje se správným kontextem požadavku a chování řízení přístupu ↗. Přečtěte si Stránka Testovací API pro další podrobnosti.
+ import { env } from "cloudflare:workers";
+ import { runInDurableObject } from "cloudflare:test";
it("does something", async () => {
- const env = getMiniflareBindings();
const id = env.OBJECT.newUniqueId();
+ const stub = env.OBJECT.get(id);
- const storage = await getMiniflareDurableObjectStorage(id);
- doSomethingWith(storage);
+ await runInDurableObject(stub, async (instance, state) => {
+ doSomethingWith(state.storage);
+ });
- const state = await getMiniflareDurableObjectState(id);
- doSomethingWith(state);
+ await runInDurableObject(stub, async (instance, state) => {
+ doSomethingWith(state);
+ });
- const instance = await getMiniflareDurableObjectInstance(id);
- await runWithMiniflareDurableObjectGates(state, async () => {
- doSomethingWith(instance);
- });
+ await runInDurableObject(stub, async (instance) => {
+ doSomethingWith(instance);
+ });
}); flushMiniflareDurableObjectAlarms() funkce byla nahrazena runDurableObjectAlarm() funkce z cloudflare:test modul. runDurableObjectAlarm() funkce přijímá jeden DurableObjectStub a vrátí Promise který se vyřeší jako true pokud byl alarm naplánovaný a alarm() handler spuštěn, nebo false jinak. Pro "flush" alarmů více instancí zavolejte runDurableObjectAlarm() ve smyčce.
+ import { env } from "cloudflare:workers";
+ import { runDurableObjectAlarm } from "cloudflare:test";
it("does something", async () => {
- const env = getMiniflareBindings();
const id = env.OBJECT.newUniqueId();
- await flushMiniflareDurableObjectAlarms([id]);
+ const stub = env.OBJECT.get(id);
+ const ran = await runDurableObjectAlarm(stub);
});Nakonec getMiniflareDurableObjectIds() funkce byla nahrazena listDurableObjectIds() funkce z cloudflare:test modul. listDurableObjectIds() funkce nyní přijímá DurableObjectNamespace instance místo jmenného prostoru string pro zajištění přísnějšího typování. Všimněte si listDurableObjectIds() funkce respektuje izolaci úložiště. ID objektů vytvořených v jiných testovacích souborech nebudou vrácena.
+ import { env } from "cloudflare:workers";
+ import { listDurableObjectIds } from "cloudflare:test";
it("does something", async () => {
- const ids = await getMiniflareDurableObjectIds("OBJECT");
+ const ids = await listDurableObjectIds(env.OBJECT);
});