I have some pretty simple Deno tests around retrieving some data from a Kv database. Using beforeAll
I open a new DB and add some test data. Then using afterAll
I close and delete the DB file.
When I run the tests, they all pass, but the describe level test fails because of this error.
getUserActions => https://jsr.io/@std/testing/1.0.4/_test_suite.ts:218:10
error: Leaks detected:
- "database" was created during the test, but not cleaned up during the test. Close the resource before the end of the test.
I can verify that the the Kv data is actually deleted by just looking at the location where it shortly lived. What am I missing here to ensure the test framework understands that the db files have been deleted.
In other words, what is causing this error?
Here are some relevant file snippets.
// The Test
import { expect } from "jsr:@std/expect";
import { afterAll, beforeAll, describe, it, } from "jsr:@std/testing/bdd";
describe("getUserActions", () => {
let kv: Deno.Kv;
beforeAll(async () => {
kv = await Deno.openKv("./test_data");
await seedTestUserActionData();
});
afterAll(async () => {
await kv.close();
console.log("start delete");
await deleteTestKv();
console.log("end delete");
});
// some tests follow
}
// database deletion functions
export async function deleteTestKv(
dbPath: string = "./test_data"
): Promise<void> {
await closeDb(dbPath);
try {
// Check if file exists before attempting deletion
await Deno.stat(dbPath);
await Deno.remove(dbPath);
await Deno.remove(dbPath + "-shm");
await Deno.remove(dbPath + "-wal");
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
console.log(`Database file ${dbPath} not found`);
} else {
console.log("Remove error:", error);
}
}
}
async function closeDb(dbPath: string): Promise<void> {
try {
// First try to close any existing connections
const kv = await Deno.openKv(dbPath);
await kv.close();
} catch (error) {
console.log("Close error:", error);
}
}
It turned out that I wasn't closing the connection in the seedTestUserActionData
function. That was the missing piece.