diff --git a/test/delete-database-file.test.ts b/test/delete-database-file.test.ts index c273828..9382505 100644 --- a/test/delete-database-file.test.ts +++ b/test/delete-database-file.test.ts @@ -45,39 +45,43 @@ describe.each([ await destroy(); }); - it('should or should not notify other instances of a delete', async () => { - let onConnectReason1: ConnectReason | null = null; - let onConnectReason2: ConnectReason | null = null; - - const db1 = new SQLocal({ - databasePath: path, - onConnect: (reason) => (onConnectReason1 = reason), - }); - const db2 = new SQLocal({ - databasePath: path, - onConnect: (reason) => (onConnectReason2 = reason), - }); - - await vi.waitUntil(() => onConnectReason1 === 'initial'); - onConnectReason1 = null; - await vi.waitUntil(() => onConnectReason2 === 'initial'); - onConnectReason2 = null; - - await db1.deleteDatabaseFile(); - - if (type !== 'memory') { - await vi.waitUntil(() => onConnectReason2 === 'delete'); - expect(onConnectReason2).toBe('delete'); - } else { - expect(onConnectReason2).toBe(null); + it( + 'should or should not notify other instances of a delete', + { timeout: type === 'opfs' ? 2000 : undefined }, + async () => { + let onConnectReason1: ConnectReason | null = null; + let onConnectReason2: ConnectReason | null = null; + + const db1 = new SQLocal({ + databasePath: path, + onConnect: (reason) => (onConnectReason1 = reason), + }); + const db2 = new SQLocal({ + databasePath: path, + onConnect: (reason) => (onConnectReason2 = reason), + }); + + await vi.waitUntil(() => onConnectReason1 === 'initial'); + onConnectReason1 = null; + await vi.waitUntil(() => onConnectReason2 === 'initial'); + onConnectReason2 = null; + + await db1.deleteDatabaseFile(); + + if (type !== 'memory') { + await vi.waitUntil(() => onConnectReason2 === 'delete'); + expect(onConnectReason2).toBe('delete'); + } else { + expect(onConnectReason2).toBe(null); + } + + expect(onConnectReason1).toBe('delete'); + + await db2.deleteDatabaseFile(); + await db2.destroy(); + await db1.destroy(); } - - expect(onConnectReason1).toBe('delete'); - - await db2.deleteDatabaseFile(); - await db2.destroy(); - await db1.destroy(); - }); + ); it('should restore user functions', async () => { const db = new SQLocal(path); diff --git a/test/overwrite-database-file.test.ts b/test/overwrite-database-file.test.ts index 6a3387f..1711901 100644 --- a/test/overwrite-database-file.test.ts +++ b/test/overwrite-database-file.test.ts @@ -83,63 +83,67 @@ describe.each([ await db2.destroy(); }); - it('should or should not notify other instances of an overwrite', async () => { - const eventValues = new Set(); - const db1 = new SQLocal({ - databasePath: path, - onConnect: (reason) => eventValues.add(`connect1(${reason})`), - }); - const db2 = new SQLocal({ - databasePath: path, - onConnect: (reason) => eventValues.add(`connect2(${reason})`), - }); - - await db2.sql`CREATE TABLE nums (num INTEGER NOT NULL)`; - await db2.sql`INSERT INTO nums (num) VALUES (123)`; - - await vi.waitUntil(() => { - return ( - eventValues.has('connect1(initial)') && - eventValues.has('connect2(initial)') - ); - }); - eventValues.clear(); - - if (type !== 'memory') { - const nums1 = await db1.sql`SELECT * FROM nums`; - expect(nums1).toEqual([{ num: 123 }]); - } - - const dbFile = await db2.getDatabaseFile(); - await db2.sql`INSERT INTO nums (num) VALUES (456)`; - await db1.overwriteDatabaseFile(dbFile, async () => { - await db1.sql`INSERT INTO nums (num) VALUES (789)`; - eventValues.add('unlock1'); - }); - - if (type !== 'memory') { - await vi.waitUntil(() => eventValues.size === 3); - expect(eventValues.has('unlock1')).toBe(true); - expect(eventValues.has('connect1(overwrite)')).toBe(true); - expect(eventValues.has('connect2(overwrite)')).toBe(true); - } else { - await vi.waitUntil(() => eventValues.size === 1); - expect(eventValues.has('unlock1')).toBe(true); + it( + 'should or should not notify other instances of an overwrite', + { timeout: type === 'opfs' ? 2000 : undefined }, + async () => { + const eventValues = new Set(); + const db1 = new SQLocal({ + databasePath: path, + onConnect: (reason) => eventValues.add(`connect1(${reason})`), + }); + const db2 = new SQLocal({ + databasePath: path, + onConnect: (reason) => eventValues.add(`connect2(${reason})`), + }); + + await db2.sql`CREATE TABLE nums (num INTEGER NOT NULL)`; + await db2.sql`INSERT INTO nums (num) VALUES (123)`; + + await vi.waitUntil(() => { + return ( + eventValues.has('connect1(initial)') && + eventValues.has('connect2(initial)') + ); + }); + eventValues.clear(); + + if (type !== 'memory') { + const nums1 = await db1.sql`SELECT * FROM nums`; + expect(nums1).toEqual([{ num: 123 }]); + } + + const dbFile = await db2.getDatabaseFile(); + await db2.sql`INSERT INTO nums (num) VALUES (456)`; + await db1.overwriteDatabaseFile(dbFile, async () => { + await db1.sql`INSERT INTO nums (num) VALUES (789)`; + eventValues.add('unlock1'); + }); + + if (type !== 'memory') { + await vi.waitUntil(() => eventValues.size === 3); + expect(eventValues.has('unlock1')).toBe(true); + expect(eventValues.has('connect1(overwrite)')).toBe(true); + expect(eventValues.has('connect2(overwrite)')).toBe(true); + } else { + await vi.waitUntil(() => eventValues.size === 1); + expect(eventValues.has('unlock1')).toBe(true); + } + + const expectedNums = [{ num: 123 }, { num: 789 }]; + const nums2 = await db1.sql`SELECT * FROM nums`; + expect(nums2).toEqual(expectedNums); + + if (type !== 'memory') { + const nums3 = await db2.sql`SELECT * FROM nums`; + expect(nums3).toEqual(expectedNums); + } + + await db1.destroy(); + await db2.deleteDatabaseFile(); + await db2.destroy(); } - - const expectedNums = [{ num: 123 }, { num: 789 }]; - const nums2 = await db1.sql`SELECT * FROM nums`; - expect(nums2).toEqual(expectedNums); - - if (type !== 'memory') { - const nums3 = await db2.sql`SELECT * FROM nums`; - expect(nums3).toEqual(expectedNums); - } - - await db1.destroy(); - await db2.deleteDatabaseFile(); - await db2.destroy(); - }); + ); it('should restore user functions', async () => { const db = new SQLocal(path); @@ -197,32 +201,36 @@ describe.each([ await destroy(); }); - it('should run onInit statements before other queries after overwrite', async () => { - const databasePath = path; - const onInit: ClientConfig['onInit'] = (sql) => { - return [sql`PRAGMA foreign_keys = ON`]; - }; - - const results: number[] = []; - - const db1 = new SQLocal({ databasePath, onInit }); - const db2 = new SQLocal({ databasePath, onInit }); - - const [{ foreign_keys: result1 }] = await db1.sql`PRAGMA foreign_keys`; - results.push(result1); - await db1.sql`PRAGMA foreign_keys = OFF`; - const [{ foreign_keys: result2 }] = await db1.sql`PRAGMA foreign_keys`; - results.push(result2); - const file = await db2.getDatabaseFile(); - await db1.overwriteDatabaseFile(file); - const [{ foreign_keys: result3 }] = await db1.sql`PRAGMA foreign_keys`; - results.push(result3); - const [{ foreign_keys: result4 }] = await db2.sql`PRAGMA foreign_keys`; - results.push(result4); - - expect(results).toEqual([1, 0, 1, 1]); - - await db1.destroy(); - await db2.destroy(); - }); + it( + 'should run onInit statements before other queries after overwrite', + { timeout: type === 'opfs' ? 1500 : undefined }, + async () => { + const databasePath = path; + const onInit: ClientConfig['onInit'] = (sql) => { + return [sql`PRAGMA foreign_keys = ON`]; + }; + + const results: number[] = []; + + const db1 = new SQLocal({ databasePath, onInit }); + const db2 = new SQLocal({ databasePath, onInit }); + + const [{ foreign_keys: result1 }] = await db1.sql`PRAGMA foreign_keys`; + results.push(result1); + await db1.sql`PRAGMA foreign_keys = OFF`; + const [{ foreign_keys: result2 }] = await db1.sql`PRAGMA foreign_keys`; + results.push(result2); + const file = await db2.getDatabaseFile(); + await db1.overwriteDatabaseFile(file); + const [{ foreign_keys: result3 }] = await db1.sql`PRAGMA foreign_keys`; + results.push(result3); + const [{ foreign_keys: result4 }] = await db2.sql`PRAGMA foreign_keys`; + results.push(result4); + + expect(results).toEqual([1, 0, 1, 1]); + + await db1.destroy(); + await db2.destroy(); + } + ); }); diff --git a/vite.config.ts b/vite.config.ts index 244ec95..d573010 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,9 +3,9 @@ import { defineConfig } from 'vite'; export default defineConfig({ test: { - testTimeout: 1500, - hookTimeout: 1500, - teardownTimeout: 1500, + testTimeout: 1000, + hookTimeout: 1000, + teardownTimeout: 1000, includeTaskLocation: true, browser: { enabled: true,