Skip to content

Commit 98b78fb

Browse files
committed
Fix tests to work in both NodeJS and browser.
1 parent 7b1076a commit 98b78fb

File tree

11 files changed

+60
-24
lines changed

11 files changed

+60
-24
lines changed

packages/api/test/src/impl-tests.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export function describeImplTests(
1212
let dbPath: string;
1313

1414
const open = async () => {
15-
// const dir = path.dirname(dbPath);
16-
// try {
17-
// await fs.mkdir(dir);
18-
// } catch (e) {}
19-
// try {
20-
// await fs.rm(dbPath);
21-
// } catch (e) {}
15+
const dir = path.dirname(dbPath);
16+
try {
17+
await fs.mkdir(dir);
18+
} catch (e) {}
19+
try {
20+
await fs.rm(dbPath);
21+
} catch (e) {}
2222
return factory(dbPath);
2323
};
2424

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { describeDriverTests } from '@sqlite-js/driver-tests';
22
import { BetterSqliteDriver } from '../../lib/index.js';
3+
import { deleteDb } from './util.js';
34

45
describeDriverTests(
56
'better-sqlite3-async-pool',
67
{ getColumns: true, rawResults: true, allowsMissingParameters: false },
7-
(path) => BetterSqliteDriver.open(path)
8+
async (path) => {
9+
await deleteDb(path);
10+
return BetterSqliteDriver.open(path);
11+
}
812
);
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { BetterSqliteDriver } from '../../lib/index.js';
22
import { describeDriverTests } from '@sqlite-js/driver-tests';
3+
import { deleteDb } from './util.js';
34

45
describeDriverTests(
56
'better-sqlite3',
67
{ getColumns: true, rawResults: true, allowsMissingParameters: false },
7-
(path) => BetterSqliteDriver.openInProcess(path)
8+
async (path) => {
9+
await deleteDb(path);
10+
return BetterSqliteDriver.openInProcess(path);
11+
}
812
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as fs from 'node:fs/promises';
2+
import * as path from 'node:path';
3+
4+
export async function deleteDb(dbPath: string) {
5+
const dir = path.dirname(dbPath);
6+
try {
7+
await fs.mkdir(dir);
8+
} catch (e) {}
9+
try {
10+
await fs.rm(dbPath);
11+
} catch (e) {}
12+
}

packages/driver-tests/src/driver-tests.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as fs from 'node:fs/promises';
2-
import * as path from 'node:path';
31
import { beforeEach, describe, test, expect } from './test.js';
42
import { SqliteDriverConnectionPool } from '@sqlite-js/driver';
53

@@ -12,20 +10,13 @@ export interface DriverFeatures {
1210
export function describeDriverTests(
1311
name: string,
1412
features: DriverFeatures,
15-
factory: (path: string) => SqliteDriverConnectionPool
13+
factory: (path: string) => Promise<SqliteDriverConnectionPool>
1614
) {
1715
describe(`${name} - driver tests`, () => {
1816
let dbPath: string;
1917

2018
const open = async () => {
21-
// const dir = path.dirname(dbPath);
22-
// try {
23-
// await fs.mkdir(dir);
24-
// } catch (e) {}
25-
// try {
26-
// await fs.rm(dbPath);
27-
// } catch (e) {}
28-
const db = factory(dbPath);
19+
const db = await factory(dbPath);
2920
return db;
3021
};
3122

packages/driver-tests/src/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface TestContext {
99
fullName: string;
1010
}
1111

12-
export const isVitest = true || process.env.VITEST == 'true';
12+
export const isVitest = process.env.VITEST == 'true';
1313
export const isMocha = !isVitest;
1414

1515
let testImpl, describeImpl, beforeEachImpl, expectImpl;
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { NodeSqliteDriver } from '@sqlite-js/driver/node';
22
import { describeDriverTests } from '../../lib/index.js';
3+
import { deleteDb } from './util.js';
34

45
describeDriverTests(
56
'node:sqlite worker',
67
{ getColumns: false, rawResults: false, allowsMissingParameters: true },
7-
(path) => NodeSqliteDriver.open(path)
8+
async (path) => {
9+
await deleteDb(path);
10+
return NodeSqliteDriver.open(path);
11+
}
812
);

packages/driver-tests/test/src/node-sqlite-sync.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { NodeSqliteDriver } from '@sqlite-js/driver/node';
22
import { describeDriverTests } from '../../lib/index.js';
33

44
import { isMocha, test } from '../../lib/test.js';
5+
import { deleteDb } from './util.js';
56

67
if (isMocha) {
78
describeDriverTests(
89
'node:sqlite direct',
910
{ getColumns: false, rawResults: false, allowsMissingParameters: true },
10-
(path) => NodeSqliteDriver.openInProcess(path)
11+
async (path) => {
12+
await deleteDb(path);
13+
return NodeSqliteDriver.openInProcess(path);
14+
}
1115
);
1216
} else {
1317
test.skip('only running in mocha');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as fs from 'node:fs/promises';
2+
import * as path from 'node:path';
3+
4+
export async function deleteDb(dbPath: string) {
5+
const dir = path.dirname(dbPath);
6+
try {
7+
await fs.mkdir(dir);
8+
} catch (e) {}
9+
try {
10+
await fs.rm(dbPath);
11+
} catch (e) {}
12+
}

packages/wa-sqlite-driver/test/src/driver.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ import { waSqlitePool } from '../../lib/index.js';
44
describeDriverTests(
55
'wa-sqlite',
66
{ getColumns: true, rawResults: true, allowsMissingParameters: false },
7-
(path) => waSqlitePool(':memory:')
7+
async (path) => {
8+
return waSqlitePool(':memory:');
9+
}
810
);

0 commit comments

Comments
 (0)