Empty KV eventhough preview KV has values during Vitest

Hello everyone,

basically the title already tells what my problem is, which I can't seem to get to work.

Basically with a CI pipeline I want to test my worker (which is basically a REST api). It should use the preview KV I am using in development.

But the KV is always empty. It seems like in my implementation it is using the production KV.

Could anyone help me with it ?

import { unstable_dev } from "wrangler";
import type { UnstableDevWorker } from "wrangler";
import { describe, expect, it, beforeAll, afterAll } from "vitest";

describe("API", () => {
  let worker: UnstableDevWorker;

  beforeAll(async () => {
    worker = await unstable_dev("src/index.ts", {
      config: "wrangler.toml",
      experimental: { disableExperimentalWarning: true },
    });
  });

  afterAll(async () => {
    await worker.stop();
  });  

it("should return test user", async () => {
    const resp = await worker.fetch("/api/v1/users/<id>", {
      headers: { "X-Auth-Token": "shared-key" },
    });
    if (resp) {
      const json = await resp.json();
      expect(json).toMatchObject({
        email: "user-mail",
      });
    }
  });
});
Was this page helpful?