UNPKG

@cloudflare/vitest-pool-workers

Version:

Workers Vitest integration for writing Vitest unit and integration tests that run inside the Workers runtime

39 lines (37 loc) 1.3 kB
import assert from "node:assert"; import { env } from "cloudflare:workers"; import { dirname } from "node:path"; import { VitestSnapshotEnvironment } from "vitest/runtime"; //#region src/worker/lib/cloudflare/snapshot.ts var WorkersSnapshotEnvironment = class extends VitestSnapshotEnvironment { #fetch(method, path, body) { const url = `http://placeholder/snapshot?path=${encodeURIComponent(path)}`; return env.__VITEST_POOL_WORKERS_LOOPBACK_SERVICE.fetch(url, { method, body }); } async prepareDirectory(dirPath) { const res = await this.#fetch("POST", dirPath); assert.strictEqual(res.status, 204); } async saveSnapshotFile(filePath, snapshot) { await this.prepareDirectory(dirname(filePath)); const res = await this.#fetch("PUT", filePath, snapshot); assert.strictEqual(res.status, 204); } async readSnapshotFile(filePath) { const res = await this.#fetch("GET", filePath); if (res.status === 404) return null; assert.strictEqual(res.status, 200); return await res.text(); } async removeSnapshotFile(filePath) { const res = await this.#fetch("DELETE", filePath); assert.strictEqual(res.status, 204); } }; var snapshot_default = new WorkersSnapshotEnvironment(); //#endregion export { snapshot_default as default }; //# sourceMappingURL=snapshot.mjs.map