Skip to content

Commit b191af3

Browse files
committed
[bun] Prepare for Bun-aware testing
1 parent e995fc5 commit b191af3

5 files changed

Lines changed: 58 additions & 15 deletions

File tree

src/tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"jsx": "react-jsx",
1111
"moduleResolution": "NodeNext",
1212
"allowSyntheticDefaultImports": true,
13-
"types": ["node", "@cloudflare/workers-types", "bun-types"],
14-
"composite": true,
13+
"noEmit": true,
1514
"allowImportingTsExtensions": true,
16-
"noEmit": true
15+
"types": ["node", "@cloudflare/workers-types", "bun-types"],
16+
"composite": true
1717
}
1818
}

test/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"jest",
1818
"node",
1919
"jest-environment-puppeteer",
20-
"http-server"
20+
"http-server",
21+
"bun-types"
2122
],
22-
"typeRoots": ["../node_modules/@types", "./modules"],
23+
"composite": true,
2324
"skipLibCheck": true,
2425
"allowJs": true
2526
}

test/unit/common/other.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const ignorable = (...args: any[]): boolean =>
1616
.match(/wasm|OPFS|ArrayBuffer|ReactDOMTestUtils|C-web|onCustomMessage/),
1717
);
1818

19+
export const isBun = process.versions.bun != null;
20+
1921
export const pause = async (ms = 50): Promise<void> =>
2022
new Promise<void>((resolve) =>
2123
setTimeout(() => setTimeout(() => setTimeout(resolve, 1), ms - 2), 1),

test/unit/core/documentation.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ import * as ws from 'ws';
4040
import * as yjs from 'yjs';
4141
import {AutomergeTestNetworkAdapter as BroadcastChannelNetworkAdapter} from '../common/automerge-adaptor.ts';
4242
import {getTimeFunctions} from '../common/mergeable.ts';
43-
import {mockFetchWasm, pause, suppressWarnings} from '../common/other.ts';
43+
import {
44+
isBun,
45+
mockFetchWasm,
46+
pause,
47+
suppressWarnings,
48+
} from '../common/other.ts';
4449

4550
const [reset, getNow] = getTimeFunctions();
4651

@@ -204,6 +209,12 @@ const prepareTestResultsFromBlock = (block: string, prefix: string): void => {
204209
};
205210

206211
describe('Documentation tests', () => {
212+
beforeAll(async () => {
213+
if (isBun) {
214+
(globalThis as any).modules['bun:sqlite'] = await import('bun:sqlite');
215+
}
216+
});
217+
207218
forEachDeepFile(
208219
'src/@types',
209220
(file) =>

test/unit/persisters/common/databases.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ import {createLibSqlPersister} from 'tinybase/persisters/persister-libsql';
2626
import {createPglitePersister} from 'tinybase/persisters/persister-pglite';
2727
import {createPostgresPersister} from 'tinybase/persisters/persister-postgres';
2828
import {createPowerSyncPersister} from 'tinybase/persisters/persister-powersync';
29+
import {createSqliteBunPersister} from 'tinybase/persisters/persister-sqlite-bun';
2930
import {createSqliteWasmPersister} from 'tinybase/persisters/persister-sqlite-wasm';
3031
import {createSqlite3Persister} from 'tinybase/persisters/persister-sqlite3';
3132
import tmp from 'tmp';
32-
import {pause, suppressWarnings} from '../../common/other.ts';
33+
import {isBun, pause, suppressWarnings} from '../../common/other.ts';
3334

3435
tmp.setGracefulCleanup();
3536
const statementMutex = new Mutex();
@@ -423,6 +424,40 @@ export const NODE_POSTGRESQL_VARIANTS: Variants = {
423424
],
424425
};
425426

427+
export const BUN_MERGEABLE_VARIANTS: Variants = {
428+
sqlite3: [
429+
async () => {
430+
const {Database} = await import('bun:sqlite');
431+
return new Database(':memory:');
432+
},
433+
['getDb', (db: typeof Database) => db],
434+
(
435+
store: Store,
436+
db: typeof Database,
437+
storeTableOrConfig?: string | DatabasePersisterConfig,
438+
onSqlCommand?: (sql: string, args?: any[]) => void,
439+
onIgnoredError?: (error: any) => void,
440+
) =>
441+
(createSqliteBunPersister as any)(
442+
store,
443+
db,
444+
storeTableOrConfig,
445+
onSqlCommand,
446+
onIgnoredError,
447+
),
448+
async (
449+
db: any,
450+
sql: string,
451+
args: any[] = [],
452+
): Promise<{[id: string]: any}[]> => db.query(sql).all(args),
453+
async (db: any) => db.close(),
454+
20,
455+
undefined,
456+
undefined,
457+
true,
458+
],
459+
};
460+
426461
export const NODE_SQLITE_VARIANTS: Variants = {
427462
...NODE_SQLITE_MERGEABLE_VARIANTS,
428463
...NODE_SQLITE_NON_MERGEABLE_VARIANTS,
@@ -438,21 +473,15 @@ export const ALL_NODE_VARIANTS: Variants = {
438473
...NODE_POSTGRESQL_VARIANTS,
439474
};
440475

441-
export const BUN_MERGEABLE_VARIANTS: Variants = {
442-
//sqlite3: NODE_SQLITE_MERGEABLE_VARIANTS.sqlite3,
443-
};
444-
445476
export const ALL_BUN_VARIANTS: Variants = {
446477
...BUN_MERGEABLE_VARIANTS,
447478
};
448479

449-
export const MERGEABLE_VARIANTS = process.versions.bun
480+
export const MERGEABLE_VARIANTS = isBun
450481
? BUN_MERGEABLE_VARIANTS
451482
: NODE_MERGEABLE_VARIANTS;
452483

453-
export const ALL_VARIANTS = process.versions.bun
454-
? ALL_BUN_VARIANTS
455-
: ALL_NODE_VARIANTS;
484+
export const ALL_VARIANTS = isBun ? ALL_BUN_VARIANTS : ALL_NODE_VARIANTS;
456485

457486
export const ADHOC_VARIANT: Variants = {
458487
adhoc: NODE_SQLITE_NON_MERGEABLE_VARIANTS.powerSync,

0 commit comments

Comments
 (0)