Skip to content

Commit 86437bb

Browse files
committed
refactor(tests): streamline test setup and cleanup, remove waitForWindows option
1 parent e76fb24 commit 86437bb

12 files changed

+154
-80
lines changed

test/backup-restoration.test.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
import { expect } from "@jest/globals";
1+
import {
2+
afterEach,
3+
beforeEach,
4+
describe,
5+
expect,
6+
it,
7+
jest,
8+
} from "@jest/globals";
29
import * as fs from "node:fs";
310
import { DatabaseSync } from "../src/index";
411
import { getTestTimeout, useTempDir } from "./test-utils";
512

613
describe("Backup Restoration", () => {
7-
const { getDbPath, closeDatabases } = useTempDir("sqlite-backup-test-", {
8-
timeout: getTestTimeout(), // Use environment-aware timeout
9-
});
14+
jest.setTimeout(getTestTimeout());
15+
const { getDbPath, closeDatabases } = useTempDir("sqlite-backup-test-");
1016

1117
let sourceDb: InstanceType<typeof DatabaseSync>;
1218
let sourceFile: string;
1319
let backupFile: string;
1420

21+
// Track all databases created in tests for proper cleanup
22+
const testDatabases = new Set<InstanceType<typeof DatabaseSync>>();
23+
1524
beforeEach(() => {
1625
sourceFile = getDbPath("source.db");
1726
backupFile = getDbPath("backup.db");
1827
sourceDb = new DatabaseSync(sourceFile);
1928
});
2029

2130
afterEach(() => {
22-
closeDatabases(sourceDb);
31+
closeDatabases(sourceDb, ...testDatabases);
32+
testDatabases.clear();
2333
});
2434

2535
it("should restore data correctly from backup", async () => {
@@ -354,9 +364,9 @@ describe("Backup Restoration", () => {
354364
// Opening corrupt database or running queries should throw
355365
try {
356366
const corruptDb = new DatabaseSync(sourceFile);
367+
testDatabases.add(corruptDb);
357368
// If opening doesn't throw, executing a query should
358369
corruptDb.exec("SELECT * FROM test");
359-
corruptDb.close();
360370
// If we get here, the test should fail
361371
expect(true).toBe(false);
362372
} catch (err: any) {

test/backup.test.ts

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { describe, expect, it } from "@jest/globals";
22
import * as fs from "node:fs";
33
import { DatabaseSync } from "../src";
4-
import { createTestDb, getTestTimeout, useTempDir } from "./test-utils";
4+
import { createTestDb, useTempDir } from "./test-utils";
55

66
describe("Backup functionality", () => {
7-
const { getDbPath, closeDatabases } = useTempDir("sqlite-backup-test-", {
8-
waitForWindows: true,
9-
timeout: getTestTimeout(), // 30 seconds base timeout for backup tests
10-
});
7+
const { getDbPath, closeDatabases } = useTempDir("sqlite-backup-test-");
118

129
let sourceDb: InstanceType<typeof DatabaseSync>;
1310
let sourcePath: string;
1411
let destPath: string;
1512

13+
// Track all databases created in tests for proper cleanup
14+
const testDatabases = new Set<InstanceType<typeof DatabaseSync>>();
15+
1616
beforeEach(() => {
1717
sourcePath = getDbPath("source.db");
1818
destPath = getDbPath("destination.db");
@@ -45,19 +45,19 @@ describe("Backup functionality", () => {
4545
});
4646

4747
afterEach(() => {
48-
closeDatabases(sourceDb);
48+
// Close all tracked databases
49+
closeDatabases(sourceDb, ...testDatabases);
50+
testDatabases.clear();
4951
});
5052

5153
it("should create a backup of the database", async () => {
5254
// Perform backup
5355
const totalPages = await sourceDb.backup(destPath);
5456
expect(totalPages).toBeGreaterThan(0);
5557

56-
// Close source database
57-
sourceDb.close();
58-
5958
// Open and verify destination database
6059
const destDb = new DatabaseSync(destPath);
60+
testDatabases.add(destDb);
6161

6262
// Check that tables exist
6363
const tables = destDb
@@ -88,22 +88,19 @@ describe("Backup functionality", () => {
8888
{ id: 2, name: "Gadget", price: 19.99 },
8989
{ id: 3, name: "Doohickey", price: 29.99 },
9090
]);
91-
92-
destDb.close();
9391
});
9492

9593
it("should handle backup with custom rate", async () => {
9694
const totalPages = await sourceDb.backup(destPath, { rate: 5 });
9795
expect(totalPages).toBeGreaterThan(0);
9896

9997
// Verify the backup
100-
sourceDb.close();
10198
const destDb = new DatabaseSync(destPath);
99+
testDatabases.add(destDb);
102100
const count = destDb
103101
.prepare("SELECT COUNT(*) as count FROM users")
104102
.get() as { count: number };
105103
expect(count.count).toBe(3);
106-
destDb.close();
107104
});
108105

109106
it("should call progress callback during backup", async () => {
@@ -129,13 +126,12 @@ describe("Backup functionality", () => {
129126
}
130127

131128
// Verify the backup completed
132-
sourceDb.close();
133129
const destDb = new DatabaseSync(destPath);
130+
testDatabases.add(destDb);
134131
const count = destDb
135132
.prepare("SELECT COUNT(*) as count FROM users")
136133
.get() as { count: number };
137134
expect(count.count).toBe(3);
138-
destDb.close();
139135
});
140136

141137
it("should handle backup to memory database", async () => {
@@ -161,8 +157,8 @@ describe("Backup functionality", () => {
161157
const totalPages = await sourceDb.backup(destPath);
162158
expect(totalPages).toBeGreaterThan(0);
163159

164-
sourceDb.close();
165160
const destDb = new DatabaseSync(destPath);
161+
testDatabases.add(destDb);
166162

167163
// Verify main database tables
168164
const tables = destDb
@@ -174,8 +170,6 @@ describe("Backup functionality", () => {
174170

175171
// Attached table should not be in the backup
176172
expect(tables.find((t) => t.name === "extra_data")).toBeUndefined();
177-
178-
destDb.close();
179173
});
180174

181175
it("should handle errors for invalid destination", async () => {
@@ -185,8 +179,14 @@ describe("Backup functionality", () => {
185179
});
186180

187181
it("should handle errors for closed database", async () => {
188-
sourceDb.close();
189-
await expect(sourceDb.backup(destPath)).rejects.toThrow(
182+
// Create a separate database for this test to avoid affecting other tests
183+
const testDbPath = getDbPath("closed-test.db");
184+
const testDb = new DatabaseSync(testDbPath);
185+
testDatabases.add(testDb);
186+
187+
// Close it and try to backup
188+
testDb.close();
189+
await expect(testDb.backup(destPath)).rejects.toThrow(
190190
"database is not open",
191191
);
192192
});
@@ -211,21 +211,19 @@ describe("Backup functionality", () => {
211211
expect(pages2).toBeGreaterThan(0);
212212

213213
// Verify both backups
214-
sourceDb.close();
215-
216214
const destDb1 = new DatabaseSync(destPath);
215+
testDatabases.add(destDb1);
217216
const count1 = destDb1
218217
.prepare("SELECT COUNT(*) as count FROM users")
219218
.get() as { count: number };
220219
expect(count1.count).toBe(3);
221-
destDb1.close();
222220

223221
const destDb2 = new DatabaseSync(destPath2);
222+
testDatabases.add(destDb2);
224223
const count2 = destDb2
225224
.prepare("SELECT COUNT(*) as count FROM users")
226225
.get() as { count: number };
227226
expect(count2.count).toBe(3);
228-
destDb2.close();
229227
});
230228

231229
it("should perform a complete backup and restore cycle", async () => {
@@ -283,6 +281,7 @@ describe("Backup functionality", () => {
283281

284282
// Open the restored database
285283
const restoredDb = new DatabaseSync(sourcePath);
284+
testDatabases.add(restoredDb);
286285

287286
// Verify all tables exist
288287
const tables = restoredDb
@@ -451,6 +450,7 @@ describe("Backup functionality", () => {
451450

452451
// Open backup and verify pragmas
453452
const backupDb = new DatabaseSync(backupPath);
453+
testDatabases.add(backupDb);
454454

455455
const backupPragmas = {
456456
journal_mode: (
@@ -574,8 +574,8 @@ describe("Backup functionality", () => {
574574
);
575575

576576
// Verify the backup is complete and valid
577-
sourceDb.close();
578577
const backupDb = new DatabaseSync(backupPath);
578+
testDatabases.add(backupDb);
579579

580580
const rowCount = backupDb
581581
.prepare("SELECT COUNT(*) as count FROM large_data")
@@ -590,8 +590,6 @@ describe("Backup functionality", () => {
590590
expect(sampleRows[0].data).toContain("Row 0:");
591591
expect(sampleRows[1].data).toContain("Row 49:");
592592
expect(sampleRows[2].data).toContain("Row 99:");
593-
594-
backupDb.close();
595593
});
596594

597595
it("should handle incremental backup simulation", async () => {
@@ -614,6 +612,7 @@ describe("Backup functionality", () => {
614612

615613
// Verify first backup has original data only
616614
const backup1Db = new DatabaseSync(backup1Path);
615+
testDatabases.add(backup1Db);
617616
const backup1Users = backup1Db
618617
.prepare("SELECT COUNT(*) as count FROM users")
619618
.get() as { count: number };
@@ -622,10 +621,10 @@ describe("Backup functionality", () => {
622621
.prepare("SELECT COUNT(*) as count FROM products")
623622
.get() as { count: number };
624623
expect(backup1Products.count).toBe(3);
625-
backup1Db.close();
626624

627625
// Verify second backup has all data
628626
const backup2Db = new DatabaseSync(backup2Path);
627+
testDatabases.add(backup2Db);
629628
const backup2Users = backup2Db
630629
.prepare("SELECT COUNT(*) as count FROM users")
631630
.get() as { count: number };
@@ -640,8 +639,6 @@ describe("Backup functionality", () => {
640639
.prepare("SELECT * FROM users WHERE name = ?")
641640
.get("David") as { id: number; name: string; email: string };
642641
expect(david).toEqual({ id: 4, name: "David", email: "david@example.com" });
643-
644-
backup2Db.close();
645642
});
646643

647644
it("should demonstrate different behavior with different rates", async () => {
@@ -716,7 +713,9 @@ describe("Backup functionality", () => {
716713

717714
// The key difference: rate=-1 should have minimal callbacks compared to others
718715
if (callbackCount2 > 0 && callbackCount3 > 0) {
719-
expect(callbackCount1).toBeLessThan(Math.max(callbackCount2, callbackCount3));
716+
expect(callbackCount1).toBeLessThan(
717+
Math.max(callbackCount2, callbackCount3),
718+
);
720719
}
721720

722721
// Ensure the backups actually completed successfully
@@ -748,25 +747,23 @@ describe("Backup functionality", () => {
748747
}
749748

750749
// All backups should produce identical databases
751-
sourceDb.close();
752-
753750
const db1 = new DatabaseSync(backup1Path);
751+
testDatabases.add(db1);
754752
const count1 = (
755753
db1.prepare("SELECT COUNT(*) as c FROM test_data").get() as { c: number }
756754
).c;
757-
db1.close();
758755

759756
const db2 = new DatabaseSync(backup2Path);
757+
testDatabases.add(db2);
760758
const count2 = (
761759
db2.prepare("SELECT COUNT(*) as c FROM test_data").get() as { c: number }
762760
).c;
763-
db2.close();
764761

765762
const db3 = new DatabaseSync(backup3Path);
763+
testDatabases.add(db3);
766764
const count3 = (
767765
db3.prepare("SELECT COUNT(*) as c FROM test_data").get() as { c: number }
768766
).c;
769-
db3.close();
770767

771768
expect(count1).toBe(200);
772769
expect(count2).toBe(200);

test/concurrent-access.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ if (process.platform === "win32" && process.env.CI) {
88

99
describe("Concurrent Access Patterns Tests", () => {
1010
const { getDbPath } = useTempDir("sqlite-concurrent-test-", {
11-
waitForWindows: true,
1211
cleanupWalFiles: true,
1312
});
1413
let dbPath: string;

test/concurrent-simple.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { describe, expect, it } from "@jest/globals";
1+
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
22
import { DatabaseSync } from "../src";
33
import { getTestTimeout, useTempDir } from "./test-utils";
44

55
describe("Simple Concurrent Access Tests", () => {
6-
const { getDbPath } = useTempDir("sqlite-concurrent-simple-", {
7-
timeout: getTestTimeout(), // Use environment-aware timeout
8-
});
6+
jest.setTimeout(getTestTimeout());
7+
const { getDbPath } = useTempDir("sqlite-concurrent-simple-");
98
let dbPath: string;
109

1110
beforeEach(() => {

test/location-method.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { beforeEach, describe, expect, jest } from "@jest/globals";
12
import * as fs from "node:fs";
23
import { DatabaseSync } from "../src/index";
3-
import { useTempDir } from "./test-utils";
4+
import { getTestTimeout, useTempDir } from "./test-utils";
45

56
describe("Enhanced location() method tests", () => {
7+
jest.setTimeout(getTestTimeout());
68
const { getDbPath } = useTempDir("node-sqlite-location-");
79
let dbPath: string;
810
let attachedDbPath: string;

0 commit comments

Comments
 (0)