testing/javascript: Clean up after test runs

This commit is contained in:
Pekka Enberg
2025-07-29 16:17:11 +03:00
parent c39f0e7557
commit 6c6d74993c
2 changed files with 43 additions and 11 deletions

View File

@@ -4,7 +4,7 @@ import fs from 'fs';
test.beforeEach(async (t) => {
const [db, errorType] = await connect();
const [db, path,errorType] = await connect();
await db.exec(`
DROP TABLE IF EXISTS users;
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)
@@ -17,14 +17,30 @@ test.beforeEach(async (t) => {
);
t.context = {
db,
path,
errorType
};
});
test.after.always(async (t) => {
test.afterEach.always(async (t) => {
// Close the database connection
if (t.context.db != undefined) {
t.context.db.close();
}
// Remove the database file if it exists
if (t.context.path) {
const walPath = t.context.path + "-wal";
const shmPath = t.context.path + "-shm";
if (fs.existsSync(t.context.path)) {
fs.unlinkSync(t.context.path);
}
if (fs.existsSync(walPath)) {
fs.unlinkSync(walPath);
}
if (fs.existsSync(shmPath)) {
fs.unlinkSync(shmPath);
}
}
});
test.serial("Open in-memory database", async (t) => {
@@ -408,12 +424,12 @@ const connect = async (path, options = {}) => {
if (provider === "turso") {
const x = await import("@tursodatabase/turso");
const db = new x.default(path, options);
return [db, x.SqliteError];
return [db, path, x.SqliteError];
}
if (provider === "libsql") {
const x = await import("libsql/promise");
const db = new x.default(path, options);
return [db, x.SqliteError, path];
return [db, path, x.SqliteError, path];
}
if (provider === "serverless") {
const x = await import("@tursodatabase/serverless");
@@ -426,7 +442,7 @@ const connect = async (path, options = {}) => {
url,
authToken,
});
return [db, x.SqliteError];
return [db, null, x.SqliteError];
}
};