Testing hono with isolated d1

I want to test my hono app using vitest and first time test runs successfully but when I rerun it db connection error occurs.

import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { drizzle, DrizzleD1Database } from "drizzle-orm/d1";
import { env, applyD1Migrations } from "cloudflare:test";
import { contributorRole } from "db/schema";
import { v7 as uuidv7 } from "uuid";

// This is now connected to your persistent local D1
let db: DrizzleD1Database;

beforeAll(async () => {
  await applyD1Migrations(env.DB, env.TEST_MIGRATIONS);

  db = drizzle(env.DB);
});

describe("ContributorRole Service", () => {
  it("should find data I already migrated", async () => {
    // lalala
    // This will now query your actual local .sqlite file
    const roles = await db
      .insert(contributorRole)
      .values({
        id: uuidv7(),
        name: "Role",
        createdAt: new Date(),
        updatedAt: new Date(),
      })
      .returning()
      .get();

    console.log(roles.name, "Role");

    expect(roles).toBeDefined();
  });
});


The error message is

workerd/jsg/util.c++:400: error: e = workerd/util/sqlite.c++:513: failed: expected _ec == SQLITE_OK [14 == 0]; unable to open database file: SQLITE_CANTOPEN


 FAIL  test/unit/services/contributor-role.service.test.ts [ test/unit/services/contributor-role.service.test.ts ]
Error: internal error; reference = rqos9gpbv3ln729c4kolon6m
....
 ❯ test/unit/services/contributor-role.service.test.ts:11:3
      9| 
     10| beforeAll(async () => {
     11|   await applyD1Migrations(env.DB, env.TEST_MIGRATIONS);
       |   ^
     12| 
     13|   db = drizzle(env.DB);
Was this page helpful?