Skip to content

Commit 25956a7

Browse files
committed
[bun] Hide from Vitest static analysis
1 parent 104af87 commit 25956a7

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

test/unit/common/other.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
import type {Database} from 'bun:sqlite';
12
import type {Id, Ids, Indexes, Metrics, Relationships} from 'tinybase';
23
import {IdObj, IdObj2} from './types.ts';
34

45
export const isBun = process.versions.bun != null;
56

7+
export const AsyncFunction = Object.getPrototypeOf(
8+
async () => null,
9+
).constructor;
10+
11+
export const importBunSqlite = new AsyncFunction(
12+
`return await import('bun:sqlite')`, // hide from Vitest static analysis
13+
) as () => Promise<{Database: typeof Database}>;
14+
615
export const pause = async (ms = 50): Promise<void> =>
716
new Promise<void>((resolve) =>
817
setTimeout(

test/unit/documentation.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ import * as ws from 'ws';
4242
import * as yjs from 'yjs';
4343
import {AutomergeTestNetworkAdapter as BroadcastChannelNetworkAdapter} from './common/automerge-adaptor.ts';
4444
import {getTimeFunctions} from './common/mergeable.ts';
45-
import {isBun, pause, suppressWarnings} from './common/other.ts';
45+
import {
46+
AsyncFunction,
47+
importBunSqlite,
48+
isBun,
49+
pause,
50+
suppressWarnings,
51+
} from './common/other.ts';
4652

4753
const [reset, getNow] = getTimeFunctions();
4854

@@ -110,7 +116,6 @@ type Results = [any, any][];
110116

111117
const resultsByName: {[name: string]: () => Promise<Results>} = {};
112118

113-
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
114119
const forEachDeepFile = (
115120
dir: string,
116121
callback: (file: string) => void,
@@ -211,7 +216,7 @@ const prepareTestResultsFromBlock = (block: string, prefix: string): void => {
211216
describe('Documentation tests', () => {
212217
beforeAll(async () => {
213218
if (isBun) {
214-
(globalThis as any).modules['bun:sqlite'] = await import('bun:sqlite');
219+
(globalThis as any).modules['bun:sqlite'] = await importBunSqlite();
215220
}
216221
});
217222

test/unit/persisters/common/databases.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ import {createSqliteBunPersister} from 'tinybase/persisters/persister-sqlite-bun
2929
import {createSqliteWasmPersister} from 'tinybase/persisters/persister-sqlite-wasm';
3030
import {createSqlite3Persister} from 'tinybase/persisters/persister-sqlite3';
3131
import tmp from 'tmp';
32-
import {isBun, noop, pause, suppressWarnings} from '../../common/other.ts';
32+
import {
33+
importBunSqlite,
34+
isBun,
35+
noop,
36+
pause,
37+
suppressWarnings,
38+
} from '../../common/other.ts';
3339

3440
tmp.setGracefulCleanup();
3541
const statementMutex = new Mutex();
@@ -424,7 +430,7 @@ export const NODE_POSTGRESQL_VARIANTS: Variants = {
424430
export const BUN_MERGEABLE_VARIANTS: Variants = {
425431
bunSqlite: [
426432
async () => {
427-
const {Database} = await import('bun:sqlite');
433+
const {Database} = await importBunSqlite();
428434
return new Database(':memory:');
429435
},
430436
['getDb', (db: typeof Database) => db],

0 commit comments

Comments
 (0)