1+ import * as path from 'node:path' ;
2+ import * as fs from 'node:fs/promises' ;
13import { Worker } from 'node:worker_threads' ;
2- import fs from 'node:fs/promises' ;
34
4- import { vi , expect , test , onTestFinished } from 'vitest' ;
5- import { AppSchema , createTempDir , databaseTest } from './utils' ;
5+ import { vi , expect , test } from 'vitest' ;
6+ import { AppSchema , databaseTest , tempDirectoryTest } from './utils' ;
67import { PowerSyncDatabase } from '../lib' ;
78import { WorkerOpener } from '../lib/db/options' ;
89
@@ -12,15 +13,14 @@ test('validates options', async () => {
1213 schema : AppSchema ,
1314 database : {
1415 dbFilename : '/dev/null' ,
15- readWorkerCount : 0 ,
16+ readWorkerCount : 0
1617 }
1718 } ) ;
1819 await database . init ( ) ;
1920 } ) . rejects . toThrowError ( 'Needs at least one worker for reads' ) ;
2021} ) ;
2122
22- test ( 'can customize loading workers' , async ( ) => {
23- const directory = await createTempDir ( ) ;
23+ tempDirectoryTest ( 'can customize loading workers' , async ( { tmpdir } ) => {
2424 const defaultWorker : WorkerOpener = ( ...args ) => new Worker ( ...args ) ;
2525
2626 const openFunction = vi . fn ( defaultWorker ) ; // Wrap in vi.fn to count invocations
@@ -29,7 +29,7 @@ test('can customize loading workers', async () => {
2929 schema : AppSchema ,
3030 database : {
3131 dbFilename : 'test.db' ,
32- dbLocation : directory ,
32+ dbLocation : tmpdir ,
3333 openWorker : openFunction ,
3434 readWorkerCount : 2
3535 }
@@ -38,8 +38,6 @@ test('can customize loading workers', async () => {
3838 await database . get ( 'SELECT 1;' ) ; // Make sure the database is ready and works
3939 expect ( openFunction ) . toHaveBeenCalledTimes ( 3 ) ; // One writer, two readers
4040 await database . close ( ) ;
41-
42- onTestFinished ( async ( ) => fs . rm ( directory , { recursive : true } ) ) ;
4341} ) ;
4442
4543databaseTest ( 'links powersync' , async ( { database } ) => {
@@ -95,6 +93,22 @@ databaseTest('can watch tables', async ({ database }) => {
9593 await expect . poll ( ( ) => fn ) . toHaveBeenCalledTimes ( 2 ) ;
9694} ) ;
9795
96+ tempDirectoryTest ( 'throws error if target directory does not exist' , async ( { tmpdir } ) => {
97+ const directory = path . join ( tmpdir , 'some' , 'nested' , 'location' , 'that' , 'does' , 'not' , 'exist' ) ;
98+
99+ expect ( async ( ) => {
100+ const database = new PowerSyncDatabase ( {
101+ schema : AppSchema ,
102+ database : {
103+ dbFilename : 'test.db' ,
104+ dbLocation : directory ,
105+ readWorkerCount : 2
106+ }
107+ } ) ;
108+ await database . waitForReady ( ) ;
109+ } ) . rejects . toThrowError ( / T h e d b L o c a t i o n d i r e c t o r y a t " .+ " d o e s n o t e x i s t / ) ;
110+ } ) ;
111+
98112databaseTest . skip ( 'can watch queries' , async ( { database } ) => {
99113 const query = await database . watch ( 'SELECT * FROM todos;' , [ ] ) [ Symbol . asyncIterator ] ( ) ;
100114 expect ( ( await query . next ( ) ) . value . rows ) . toHaveLength ( 0 ) ;
0 commit comments