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

View File

@@ -3,7 +3,7 @@ import crypto from 'crypto';
import fs from 'fs'; import fs from 'fs';
test.beforeEach(async (t) => { test.beforeEach(async (t) => {
const [db, errorType, provider] = await connect(); const [db, path, provider, errorType] = await connect();
db.exec(` db.exec(`
DROP TABLE IF EXISTS users; DROP TABLE IF EXISTS users;
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT) CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)
@@ -16,15 +16,31 @@ test.beforeEach(async (t) => {
); );
t.context = { t.context = {
db, db,
path,
provider,
errorType, errorType,
provider
}; };
}); });
test.after.always(async (t) => { test.afterEach.always(async (t) => {
// Close the database connection
if (t.context.db != undefined) { if (t.context.db != undefined) {
t.context.db.close(); 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) => { test.serial("Open in-memory database", async (t) => {
@@ -451,17 +467,17 @@ const connect = async (path, options = {}) => {
if (provider === "turso") { if (provider === "turso") {
const x = await import("@tursodatabase/turso/sync"); const x = await import("@tursodatabase/turso/sync");
const db = new x.default(path, options); const db = new x.default(path, options);
return [db, x.SqliteError, provider]; return [db, path, provider, x.SqliteError];
} }
if (provider === "libsql") { if (provider === "libsql") {
const x = await import("libsql"); const x = await import("libsql");
const db = new x.default(path, options); const db = new x.default(path, options);
return [db, x.SqliteError, provider, path]; return [db, path, provider, x.SqliteError];
} }
if (provider == "better-sqlite3") { if (provider == "better-sqlite3") {
const x = await import("better-sqlite3"); const x = await import("better-sqlite3");
const db = x.default(path, options); 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); throw new Error("Unknown provider: " + provider);
}; };