From 6c6d74993c48cbae3ff3dd1909849e056df1377a Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 29 Jul 2025 16:17:11 +0300 Subject: [PATCH] testing/javascript: Clean up after test runs --- testing/javascript/__test__/async.test.js | 26 +++++++++++++++++---- testing/javascript/__test__/sync.test.js | 28 ++++++++++++++++++----- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/testing/javascript/__test__/async.test.js b/testing/javascript/__test__/async.test.js index 9ae77cf56..2b6264d84 100644 --- a/testing/javascript/__test__/async.test.js +++ b/testing/javascript/__test__/async.test.js @@ -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]; } }; diff --git a/testing/javascript/__test__/sync.test.js b/testing/javascript/__test__/sync.test.js index bdcf6e90e..d71e37ba4 100644 --- a/testing/javascript/__test__/sync.test.js +++ b/testing/javascript/__test__/sync.test.js @@ -3,7 +3,7 @@ import crypto from 'crypto'; import fs from 'fs'; test.beforeEach(async (t) => { - const [db, errorType, provider] = await connect(); + const [db, path, provider, errorType] = await connect(); db.exec(` DROP TABLE IF EXISTS users; CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT) @@ -16,15 +16,31 @@ test.beforeEach(async (t) => { ); t.context = { db, + path, + provider, errorType, - provider }; }); -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) => { @@ -451,17 +467,17 @@ const connect = async (path, options = {}) => { if (provider === "turso") { const x = await import("@tursodatabase/turso/sync"); const db = new x.default(path, options); - return [db, x.SqliteError, provider]; + return [db, path, provider, x.SqliteError]; } if (provider === "libsql") { const x = await import("libsql"); const db = new x.default(path, options); - return [db, x.SqliteError, provider, path]; + return [db, path, provider, x.SqliteError]; } if (provider == "better-sqlite3") { const x = await import("better-sqlite3"); const db = x.default(path, options); - return [db, x.default.SqliteError, provider]; + return [db, path, provider, x.default.SqliteError]; } throw new Error("Unknown provider: " + provider); };