1+ import * as path from 'node:path' ;
12import { Worker } from 'node:worker_threads' ;
2- import fs from 'node:fs/promises' ;
33
4- import { vi , expect , test , onTestFinished } from 'vitest' ;
5- import { AppSchema , createTempDir , databaseTest } from './utils' ;
4+ import { vi , expect , test } from 'vitest' ;
5+ import { AppSchema , databaseTest , tempDirectoryTest } from './utils' ;
66import { PowerSyncDatabase } from '../lib' ;
77import { WorkerOpener } from '../lib/db/options' ;
88
@@ -19,8 +19,7 @@ test('validates options', async () => {
1919 } ) . rejects . toThrowError ( 'Needs at least one worker for reads' ) ;
2020} ) ;
2121
22- test ( 'can customize loading workers' , async ( ) => {
23- const directory = await createTempDir ( ) ;
22+ tempDirectoryTest ( 'can customize loading workers' , async ( { tmpdir} ) => {
2423 const defaultWorker : WorkerOpener = ( ...args ) => new Worker ( ...args ) ;
2524
2625 const openFunction = vi . fn ( defaultWorker ) ; // Wrap in vi.fn to count invocations
@@ -29,7 +28,7 @@ test('can customize loading workers', async () => {
2928 schema : AppSchema ,
3029 database : {
3130 dbFilename : 'test.db' ,
32- dbLocation : directory ,
31+ dbLocation : tmpdir ,
3332 openWorker : openFunction ,
3433 readWorkerCount : 2
3534 }
@@ -38,8 +37,6 @@ test('can customize loading workers', async () => {
3837 await database . get ( 'SELECT 1;' ) ; // Make sure the database is ready and works
3938 expect ( openFunction ) . toHaveBeenCalledTimes ( 3 ) ; // One writer, two readers
4039 await database . close ( ) ;
41-
42- onTestFinished ( async ( ) => fs . rm ( directory , { recursive : true } ) ) ;
4340} ) ;
4441
4542databaseTest ( 'links powersync' , async ( { database } ) => {
@@ -95,6 +92,19 @@ databaseTest('can watch tables', async ({ database }) => {
9592 await expect . poll ( ( ) => fn ) . toHaveBeenCalledTimes ( 2 ) ;
9693} ) ;
9794
95+ tempDirectoryTest ( 'automatically creates directory' , async ( { tmpdir} ) => {
96+ const database = new PowerSyncDatabase ( {
97+ schema : AppSchema ,
98+ database : {
99+ dbFilename : 'test.db' ,
100+ dbLocation : path . join ( tmpdir , 'some' , 'nested' , 'location' , 'that' , 'does' , 'not' , 'exist' ) ,
101+ readWorkerCount : 2
102+ }
103+ } ) ;
104+
105+ await database . get ( 'SELECT 1;' ) ; // Make sure the database is ready and works
106+ } ) ;
107+
98108databaseTest . skip ( 'can watch queries' , async ( { database } ) => {
99109 const query = await database . watch ( 'SELECT * FROM todos;' , [ ] ) [ Symbol . asyncIterator ] ( ) ;
100110 expect ( ( await query . next ( ) ) . value . rows ) . toHaveLength ( 0 ) ;
0 commit comments